What does “the promise of fire at the same turn of the cycle of events” mean?

New to NodeJS. After going through a promise ("promise is 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'); 

Exit:

 FIRST SECOND 

I do not understand, I would think that since the allowed shot is first, I must first see the second.

They explain that this is because "Promise fires at the same turn of the event loop." I do not understand what it means...

+1
javascript q
source share
2 answers

Basically, the point is that the then promise handler will not work until the current code stream completes execution and control returns to the runtime (in this case, Node).

This is an important feature of Promises / A + compatible promises because it provides predictability. Regardless of whether the solution immediately promises:

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

or 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 sure that FIRST will always be registered first.

This is discussed in detail in chapters 2 and 3. You do not know JS - asynchrony and performance . Asynchronous operations, which are sometimes performed asynchronously and sometimes are executed synchronously, are called Zalgos, and they are not considered good.

It is important to note that the widely used promises in jQuery do not obey this behavior and have a number of other problems . If you find yourself with a jQuery promise, wrap it in the correct promise and continue:

 Q($.ajax(...)).then(breatheWithEase); 
+8
source share

They explain that this is because "Promise fires at the same turn of the event loop." I do not understand what it means...

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

What is more important to understand is the following sentence in this paragraph:

You can expect that the functions passed to the then method of the promise will be called in the NEXT inverse event loop.

This is like a simple rule of thumb: then callbacks are always called asynchronously. Always, at every fulfillment of promises; It is defined by Promises / A + specification .

Why is this? For consistency. Unlike your example snippet, you don’t know how and when the promise will be resolved - you just get it back from any method that you called. And it may have already been resolved, or it may not yet be considered. Now you call the promise method then , and you may know: it will be asynchronous. You do not need to deal with cases where it can be synchronous or not and change the meaning of your code, it is simply always asynchronous.

+1
source share

All Articles