.then returns 'world' before 'hello'

30 views
Skip to first unread message

adrian...@gmail.com

unread,
Aug 2, 2013, 5:06:55 AM8/2/13
to q-con...@googlegroups.com
I'm sorry if it's too basic of a question, I'm trying to get a grip on the topic. Any help is appreciated:

coffee> Q = require 'q'
#... many module loading lines commented out


------> sayHello = -> ....... console.log 'hello' [Function]
------> a = -> ....... setTimeout sayHello, 1000 [Function]
 
------> Q.fcall(a())
....... .then console.log 'world'
world
[object Object]
coffee> hello
 
undefined



Domenic Denicola

unread,
Aug 2, 2013, 9:34:02 AM8/2/13
to q-con...@googlegroups.com

Since `a()` does not return a promise, `Q.fcall(a())` gives you back a promise that is immediately fulfilled, so its onFulfilled handler is called after only a single turn of the event loop, logging “world.” Then, 1000 ms later, your setTimeout timer fires independent of the Q mechanism, and “hello” is logged.

 

If you want to return a promise to log “hello” after 1000 ms, you’ll want to do something like

 

var Q = require("q");

 

function sayHello() {

  console.log("hello");

}

 

function a() {

  return Q.delay(1000).then(sayHello);

}

 

a().then(function () {

  console.log("world");

});

 

Note that you don’t need to use `Q.fcall` if `a()` already returns a promise.

 

Also, just a tip, when asking for help on mailing lists it is preferable to use the common language everyone speaks and that the library is written in, viz. JavaScript. Using CoffeeScript will dramatically decrease the number of people able to help you or interested in responding.

 

--
You received this message because you are subscribed to the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to q-continuum...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

adrian...@gmail.com

unread,
Aug 3, 2013, 1:54:35 AM8/3/13
to q-con...@googlegroups.com
Thank you very much for your response, and I'll keep the mailing list etiquette tip in mind as well!
Reply all
Reply to author
Forward
0 new messages