In Promises, Is The Callback Order Guaranteed?
Solution 1:
If you call "then" on the same promise multiple times (no chaining), the resolver functions will be called in the same order they were added.
The ECMAScript 2015 specs state that "reactions" are enqueued in insertion order if a promise is resolved.
http://www.ecma-international.org/ecma-262/6.0/#sec-triggerpromisereactions
25.4.1.8 TriggerPromiseReactions ( reactions, argument )
The abstract operation TriggerPromiseReactions takes a collection of PromiseReactionRecords and enqueues a new Job for each record. Each such Job processes the [[Handler]] of the PromiseReactionRecord, and if the [[Handler]] is a function calls it passing the given argument.
Repeat for each reaction in reactions, in original insertion order
a. Perform EnqueueJob("PromiseJobs", PromiseReactionJob, «reaction, argument»).
This means that your resolver functions will be called in the order they were added (in your case, from 0 to 19).
Solution 2:
From specs :
If the value of promise's [[PromiseState]] internal slot is "pending",
- Append fulfillReaction as the last element of the List that is the value of promise's [[PromiseFulfillReactions]] internal slot.
So yes, by calling multiple time then()
, you are assured that the callback order will be the same as the one you invoked.
Post a Comment for "In Promises, Is The Callback Order Guaranteed?"