Why are Q.js promises asynchronous after they have been resolved?

前端 未结 2 494
一向
一向 2021-01-18 07:43

If I have the following :

var deferred = Q.defer();

deferred.resolve();

var a = deferred.promise.then(function() {
    console.log(1);    
});

console.log         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-18 08:28

    Answer is consistency.

    In real code, you don't have promises that are always immediately resolved when created, they would be pointless. So you have promises that sometimes may be immediately resolved.

    In that case, you don't want to have a different flow. You want always the same, predictable flow. So you want the next function to be always called on next tick.

    Don't use a promise when you don't need one.

提交回复
热议问题