LastTribunal
LastTribunal

Reputation: 1

Combining promises with Q

In jquery, I can combine promises as follows:

var Promise = $.when(func1.execute(), func2.execute());
Promise.done(function (data1, data2) {
// code here
}

How would you rewrite this using Q?

Also. what are the benefits of using Q in this scenario over jquery?

Upvotes: 3

Views: 2469

Answers (2)

ForbesLindesay
ForbesLindesay

Reputation: 10702

Simple Answer:

var promise = Q.all([func1.execute(), func2.execute()]);
promise.spread(function (data1, data2) {
  // code here
})
.done();

Annotated:

//make `promise` be a promise for the array
var promise = Q.all([func1.execute(), func2.execute()]);

//use spread to spread the array across the arguments of a function
promise.spread(function (data1, data2) {
  // code here
})
//use done so errors are thrown
.done();

The concepts in Q are much more clearly defined. The promise represents a single value that may not be available yet. This parallels synchronous code very precisely.

To represent multiple values then, we simply use an array. Q.all is a helper method that takes an array of promises, and returns a promise for an array containing their fulfilled values. If any promise in the array is rejected, the resulting promise will also be rejected.

Because in this case we know exactly how long the array is and essentially just want the items separately, it's convenient to use the spread helper provided by Q. When called on a promise for an array, spread takes the items in the array and spreads them out across all the arguments of the function. You could simply have done:

//make `promise` be a promise for the array
var promise = Q.all([func1.execute(), func2.execute()]);
promise.done(function (data) {
  var data1 = data[0];
  var data2 = data[1];
  // code here
});

This is perhaps conceptually simpler, but considerably less clean. I strongly recommend you read through https://github.com/kriskowal/q/wiki/Coming-from-jQuery as this has some really helpful advice to people coming from jQuery. If you find anything in it that can be improved, you can edit it and hopefully help future people making the same transition.

Upvotes: 6

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

There's nothing in the question that can't be discovered after a few minutes' research, however here goes :

Combining promises

https://github.com/kriskowal/q/wiki/API-Reference#promise-for-array-methods

A comparison of Q and jQuery

Use jQuery or Q.Js for promises

Although it's far from a universal truth, you will tend to find that Q is preferred in server-side Node.js programming, while jQuery is preferred for use client-side (in browsers).

If, like me, you come to Q having already learned jQuery, then you will find Q very confusing. In particular, the statement from the Promises/A proposal :

if the callback throws an error, the returned promise will be moved to failed state

is interpreted figuratively in jQuery and literally in Q. Hence, while Q places an emphasis on throwing/catching errors (both in actuality and by analogy in the naming of some of its methods), no such emphasis, indeed no such concept, exists in jQuery.

The above statement is more formally expressed at section 3.2.4.6.2 of the [Promises/A+] proposal, leading to a rejection model which (together with the rest of section section 3.2.4.6) you will regard as totally correct or unnecessarily inflexible depending on your point of view. As I understand it, Q conforms with section 3.2.4.6; jQuery does not.

Upvotes: 0

Related Questions