Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Q promise: are callbacks invoked in the same order as registered?

I'm using the Q promise library. My code relies on the fact that the callbacks for a single promise are executed in the same order as they were registered.

http://jsfiddle.net/HgYtK/1/

var deferred = Q.defer();
var promise = deferred.promise;

['first', 'second', 'third'].forEach(function (position) {
  promise.then(function () {
    alert(position);
  });
});

deferred.resolve();

This does produce the correct result, but I don't know if it's part of the spec or a happy coincidence that could break down the line.

like image 213
Ilia Choly Avatar asked May 14 '13 18:05

Ilia Choly


Video Answer


1 Answers

From the Promises/A+ Spec

2.2.6.1

If/when promise is fulfilled, respective onFulfilled callbacks must execute in the order of their originating calls to then.

like image 131
Ilia Choly Avatar answered Jan 25 '23 22:01

Ilia Choly