promises, Q, deferred

29 views
Skip to first unread message

Reza Razavipour

unread,
Oct 21, 2013, 12:16:35 PM10/21/13
to defer...@googlegroups.com
Can you explain the relationships? What do I use deferred for?

Thanks
Reza


Mariusz Nowak

unread,
Oct 21, 2013, 1:41:20 PM10/21/13
to defer...@googlegroups.com
Firstly, Promise and Deferred are interfaces (concepts).

It was well described by Kris Kowal in his JSConf presentation: http://youtu.be/IbDF3UdgOb0?t=16m35s and I also tried to do my best later in one of my presentations: http://www.medikoo.com/asynchronous-javascript/#deferred

So:
Promise is an object that represents eventual value which may already be available or is expected to be available in a future, and technically having a promise object you should not be able to affect it's state (resolve it or reject it).

Deferred is an object that provides access to both promise resolver and promise itself. So if you're producing a promise, you create a deferred, return promise to consumers and when it's the time you resolve it via resolver.

Still, that was the picture of promises in 2010, since then we've come a long way, and now there's an idea of Promise constructor that provides full functionality of Deferred, but doesn't create any Deferred instances:

var promise = new Promise(function (resolve, reject) {
   // when ready call either resolve or reject
});

So, Deferred concept was recently replaced with above idea. Still as it's very recent change many libraries (including Q and Deferred) offer older way of handling promise objects (using both Deferred and Promise interfaces).

When speaking of Deferred and Q libraries:
Q was a first solid well thought promise library in JavaScript, it actually paved the path for hundreds of other promise libraries that we have now.
Deferred at some point was created as an alternative to Q, as I found Q to be limited for use cases I had. At that time it didn't offer any generic solution to work with node.js callbacks, and put a lot of focus on some remote promises idea, which I didn't find compelling. I also felt API could be simpler and I did my best to achieve that with Deferred. 

So both libraries are complete promise libraries, at it's core they share same concept of promise flow, but they differ in API's.  Pick what works better for you.

Mariusz

--
You received this message because you are subscribed to the Google Groups "deferred-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deferred-js...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reza Razavipour

unread,
Oct 21, 2013, 6:11:41 PM10/21/13
to defer...@googlegroups.com
Why do I get a syntax error when I type your code snippet?

var promise = new Promise(function (resolve, reject) {
   // when ready call either resolve or reject
});

ReferenceError: Promise is not defined




Mariusz Nowak

unread,
Oct 22, 2013, 2:36:24 AM10/22/13
to defer...@googlegroups.com
Promise constructor is not implemented natively anywhere yet. Have you imported library that provides such constructor and assigned it to `Promise` variable?

btw. I think in first place you need to devote some time to read documentations of libraries you want to use, as most of your questions are answered there.


Reply all
Reply to author
Forward
0 new messages