Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery or Q.Js for promises

I'm looking into BreezeJs and there samples are using Q.js for promises to handle asynchronous calls. John Papa is also using Q. JQuery has promises as well. What are the differences between the two?

like image 694
AlignedDev Avatar asked Nov 28 '12 17:11

AlignedDev


People also ask

Why do we need promises in jQuery?

promise() method returns a dynamically generated Promise that is resolved once all actions of a certain type bound to the collection, queued or not, have ended. By default, type is "fx" , which means the returned Promise is resolved when all animations of the selected elements have completed.

Does jQuery get return a Promise?

jQuery objects can now return a Promise to observe when all animations on a collection have completed.” jQuery provides several methods that support custom animations (for example, the animate(), fadeIn(), and slideUp() methods). Their return value is the jQuery object.

How use jQuery deferred and Promise?

If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point. Return only the Promise object via deferred. promise() so other code can register callbacks or inspect the current state. For more information, see the documentation for Deferred object.

What is Promise in Javascript?

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.


2 Answers

Both are based on the Promises/A standard and implement a then method (though only current jQuery, they once had a incompatible pipe instead of then). However, there are a few differences:

  • Q has exception handling. All thrown errors in the async then callbacks will be caught and reject the promise (and will only get re-thrown if you call .end()). Not sure whether I personally like that. It's the standardized way which jQuery does not follow, rejecting from then in jQuery deferreds is much more complicated.
  • Q promises are resolved with a single value/reason (like you return/throw it from then), while jQuery allows multiple arguments in resolve/reject calls on its Deferreds.
  • Q has lots of Proxy methods which will allow you to modifiy future values
  • Q has .all and similiar, which are more complicated with jQuery ($.when.apply($, […])).
  • Q does explicitly work with ticks in the event loop and guarantees asynchronity, while jQuery can be synchronous as well. This is now required by the Promises A/+ specification.

… which is basically Promises/B. As you can see, the Q API is more powerful, and (imho) better designed. Depending on what you want to do, Q could be the better choice, but maybe jQuery (especially if already included) is enough.

like image 96
Bergi Avatar answered Oct 03 '22 17:10

Bergi


JQuery's promise implementation of the Promises/A spec has some real issues. The following link describes them far better than I can: missing-the-point-of-promises

like image 23
Jay Traband Avatar answered Oct 03 '22 18:10

Jay Traband