Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#814·q

Q.when() is not resolved synchronously while Q() is

Author: jesantanaCreated May 27, 2017Updated Jul 28, 2017

I have a code similar to this in one of my tests.

var Q=require('q');

var staticWhen=Q.when(1);
var staticQ=Q(1);

function printStatusInfo(prom){
    console.log(prom.isPending());
    console.log(prom.isFulfilled());
    console.log(prom.inspect());
}

printStatusInfo(staticWhen);
printStatusInfo(staticQ);

and this outputs

staticWhen
true
false
{ state: 'pending' }

staticQ
false
true
{ state: 'fulfilled', value: 1 }

Only if you wait for the next tick you can have the static when promise fulfilled, while the static Q is fulfilled from the beginning.

I think this can be a problem if you substitute Q.when() for Q(), for example in a test that synchronously assert that the promise is fulfilled. Maybe it should be pointed in the docu that they behave differently in this case, because in the first paragraph of the reference says that they are equivalent.

Source: kriskowal/q

View original on GitHubView discussion on GitHub