13

What are the pros & cons of Javascript Promises vs jQuery Deferred Objects?

For example, what issues do they each have, if any?

A fellow stack overflow member stated:

"...real promises are real, and fake promises are fake."

What did he mean by this?

picokol
  • 174
  • 1
  • 10
  • Some guy probably is correct, but this is not a good way to settle the argument. Pistols at dawn. – Pointy Sep 28 '15 at 20:23
  • Some guy is mistaken, purely since IE doesn't support javascript Promises and it's not yet mature. in the future he will be right. – Saar Sep 28 '15 at 20:24
  • @Pointy, lol, well I'm legitimately looking for an answer (not trying to debate this). If he's right, I'd like to know why, and not rely on his cryptic response. – picokol Sep 28 '15 at 20:24
  • @picokol you can't write something that 40% of the users can't work with (javascript Promises in IE). – Saar Sep 28 '15 at 20:25
  • 1
    @Saar you're correct in that native Promises aren't universally supported, but there are important differences in the way native Promises work from the way jQuery Promise/Deferred works. – Pointy Sep 28 '15 at 20:26
  • @Pointy, I know. that's why I said in the future he will be correct. – Saar Sep 28 '15 at 20:26
  • 1
    Have a look at [this list of actual differences](http://stackoverflow.com/q/13610741/1048572), and then the many [Problems inherent to jQuery $.Deferred](http://stackoverflow.com/q/23744612/1048572). – Bergi Sep 28 '15 at 21:25
  • @Saar, Apparently Microsoft Edge, (IE's replacement) does support javascript Promises. What do you think? The gist I'm getting from everyone's comments & links are that native promises are better because they follow the standard and don't have issues with things like error handling. So, I'm leaning toward the idea that Javascript Promises are indeed better. – picokol Sep 28 '15 at 21:50

2 Answers2

16

There is no such thing as a "real promise" or a "fake promise". There are promise implementations that follow the current standards and there are promise implementations that do not follow the current standards. There is no inherent attribute of a promise that says it's "real" or "fake". They are bits of Javascript that implement a standard behavior.

Any promise implementation that rigorously follows the standard should be fine to use and should be interoperable with other standard promise implementations. Most will consider interoperability and adherence to accepted standards a useful characteristic.

Now, jQuery promises do not follow the promise standards which is where some people talk bad of jQuery promises. They are apparently working on making them more standard for a future version of jQuery, but they currently deviate from the standards in many ways. This leads to issues in that you have to code differently when using jQuery promises than when using standards promises. So jQuery promise code does not look the same as ES6 promise code.

Problems in jQuery Promises

See this reference for a list of problems with the current jQuery promises. The two main ones are a problem with error handling in rejected promises and an inconsistency in execution order of .then() callbacks which can lead to unpredictable or inconsistent code execution. All .then() handlers are supposed to be executed async (after the current thread of JS unwinds). jQuery promises do not always do this and it can cause problems in some types of code.

State of Native Promises in Browsers

Native promises are a somewhat new thing in browsers, so it's not yet the case that you can just rely on the fact that all browsers that might hit your site will necessarily have native promises built in. Native promises are built into browsers starting with Safari 7.1, Firefox 29, Chrome 32, Edge, Android 4.4.4, IOS 8.4 and not available yet in IE (as of Sept, 2015). So, you still need a fairly recent browser in order to have native promise support. As such, there are many excellent promise libraries that can offer either a polyfill or a full-on replacement, thus giving your code the ability to use promises in any browser with really no compromise vs. native promises.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
3

JavaScript Promises are better because it conforms to the standard. One day they will be available on all browsers natively. For now you can use something like babel to use ES6 Promises today: https://babeljs.io/docs/learn-es2015/#promises.