What does “Promise fires on the same turn of the event loop” mean?

戏子无情 提交于 2019-12-10 23:03:13

问题


New to NodeJS. Going through the promise tutorial ('promise-it-wont-hurt') I have the following script:

var Q = require('q');
var deferred = Q.defer();
deffered.resolve('SECOND');
deffered.promise.then(console.log);
console.log('FIRST');

The output:

FIRST
SECOND

I don't get it, I would have thought that since resolved is fired first I should see second first.

They explain that this happens because of 'Promise fires on the same turn of the event loop'. I don't understand what that means...


回答1:


Basically, the point is that the promise's then handler will not run before the current flow of code has finished executing and control is returned to the execution environment (in this case Node).

This is an important characteristic of Promises/A+ compliant promises because it ensures predictability. Regardless of whether the promise resolves immediately:

function getAPromise() {
    var Q = require('q');
    var deferred = Q.defer();
    deferred.resolve('SECOND');
    return deferred.promise;
}

getAPromise().then(console.log);

console.log('FIRST');

or whether it resolves after 10 seconds:

function getAPromise() {
    var Q = require('q');
    var deferred = Q.defer();
    setTimeout(function () {
        deferred.resolve('SECOND');
    }, 10000);
    return deferred.promise;
}

getAPromise().then(console.log);

console.log('FIRST');

you can be assured that FIRST will always be logged first.

This is discussed at length in chapters 2 and 3 of You don't know JS - async & performance. Asynchronous operations that sometimes run asynchronously and sometimes run synchronously are called Zalgos, and they are not considered to be a good thing.

It's important to note that the widely used promises in jQuery do not obey this behavior and have a number of other problems as well. If you find yourself with a jQuery promise, wrap it in a proper promise and proceed:

Q($.ajax(...)).then(breatheWithEase);



回答2:


They explain that this happens because of 'Promise fires on the same turn of the event loop'. I don't understand what that means...

Me neither, imho this part doesn't make much sense. It probably should mean that when you call resolve or reject, the promise is settled immediately, and from then on will not change its state. This has nothing to do with the callbacks yet.

What is more important to understand is the next sentence in that paragraph:

You can expect that the functions passed to the "then" method of a promise will be called on the NEXT turn of the event loop.

It's like a simple rule of thumb: then callbacks are always called asynchronously. Always, in every proper promise implementation; this is mandated by the Promises/A+ specification.

Why is this? For consistency. Unlike in your example snippet, you don't know how or when a promise is resolved - you just get handed it back from some method you called. And it might have already been resolved, or it might be still pending. Now you call the promise's then method, and you can know: it will be asynchronous. You don't need to deal with cases that might be synchronous or not and change the meaning of your code, it simply is always asynchronous.



来源:https://stackoverflow.com/questions/28749801/what-does-promise-fires-on-the-same-turn-of-the-event-loop-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!