jQuery 3.0 promises

406 Views Asked by At

What do you think about the following?

var starter;
starter = $.Deferred();

starter.then(function () {
    console.log('promiseOne done');
});

starter.resolve();

var now=new Date().getTime();
var stop=now+5000;
while (stop>new Date().getTime()){}
console.log('main thread finished');

In jQuery-1.11.1, we get:

promiseOne done
main thread finished

which is obviously wrong and in jQuery 3.0, we get:

main thread finished
promiseOne done

which is hopefully what we expect, since promise callbacks are executed asynchronously.

However, by replacing .then with .done, in both cases I am getting:

promiseOne done
main thread finished

What is going on here? Is that still a bug of jQuery 3.0 or .done callbacks are executed synchronously?

1

There are 1 best solutions below

3
On

You should not use .done as long as you can except in order to terminate promise chains appropriately. .done is not specified in Promises/A+ so there are no issues regarding it.

The promise callbacks are executed asynchronously in order to fix inherent problems in the previous design.