node.js - Q.all doesn't call tasks -
so have coffeescript (simplified focus on real problem)
q = require 'q' events = require 'events' class someobj extends events.eventemitter constructor: () -> settimeout () => @emit 'done' , 3000 class someobj2 extends events.eventemitter constructor: () -> settimeout () => @emit 'done' , 50000 class main someobj1: null someobj2: null constructor: () -> q.all([ => @task1(), => @task2()]) .then (results)-> console.log 'results' console.log results .catch((error)-> console.log 'error' console.log error ) task1: () -> console.log 'task1 started' defer = q.defer() @someobj = new someobj() @someobj.on 'done', (err, data) => console.log 'task1 done' defer.resolve data return defer.promise task2: () -> console.log 'task2 started' defer = q.defer() @someobj2 = new someobj2() @someobj2.on 'done', (err, data) => console.log 'task2 done' defer.resolve data return defer.promise main = new main()
the output is:
results [ [function], [function] ]
in main::constructor
, callbacks @task1
, @task2
doesn't seem called. sure this, i've had added console.log
@ top of both of them. , don't printed out, can sure they're not called.
for testing purposes, replaced block
constructor: () -> q.all([ => @task1(), => @task2()]) .then (results)-> console.log 'results' console.log results .catch((error)-> console.log 'error' console.log error )
by block
constructor: () -> q.fcall () => @task1() .then () => @task2() .then (results)-> console.log 'results' console.log results .catch((error)-> console.log 'error' console.log error )
and works espected, it's not want. goal start task1 , 2 in parallel.
side note: inside tasks, able use @
member variables of main
what's wrong?
q.all
helper expects array or promises resolve, not array of functions. means should call tasks if want use q.all
.
in example, removing anonymous function wrappings trick:
constructor: () -> q.all([ @task1() @task2() ]).then (results) -> // success .catch (error) -> // error
Comments
Post a Comment