1

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?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Unknown developer
  • 5,414
  • 13
  • 52
  • 100
  • 1
    Read this [blog post](http://blog.jquery.com/2016/01/14/jquery-3-0-beta-released/) there are some changes in the defered object. – Alon Eitan Feb 18 '16 at 13:19
  • no mention of `.done` - perhaps they've spent all their time making Deferred.then Promise/A+ compliant and didn't give a monkeys about all the other cruft they put on Deferred – Jaromanda X Feb 18 '16 at 13:27
  • @JaromandaX fwiw, attempted to query this, that is the differences between `.done()` and `.then()` , at jquery irc today; the responses, in general, is that the spec only requires a `.then()` method, and that `.done()` should not be considered equivalent to `.then()` . http://irc.jquery.org/%23jquery/latest.log.html – guest271314 Feb 19 '16 at 18:03

1 Answers1

2

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.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I'd like you to have a look at my [bounty](http://stackoverflow.com/questions/35362458/animations-under-single-threaded-javascript) in which no one gave me a good answer. I am considering you an expert in promises and you may answer it... – Unknown developer Feb 18 '16 at 22:00
  • I've posted an answer @ILIAS - let me know if that's what you had in mind. – Benjamin Gruenbaum Feb 18 '16 at 23:14
  • So, what do you think about .done callbacks? Are they executed synchronously or not? By the way, thank you for your immediate response to my bounty. – Unknown developer Feb 19 '16 at 08:49