I have N number of groups of promises and I simply want to run all promises in group 1, and then when all successful, all the promises in group 2, etc up to group N.
My promises are all wrapping traditional async calls so I'm using Q's defer functionality:
createPromise: function (name, duration) {
return function () {
console.log('Executing ' + name);
var deferred = Q.defer();
setTimeout(function () {
console.log(name + ' resolved');
deferred.resolve();
}, duration);
return deferred.promise;
};
},
Here's an example with 2 groups:
var group1 = [
this.createPromise('A', 5000), // 5 second delay
this.createPromise('B', 6000)
];
var group2 = [
this.createPromise('1', 1000), // 1 second delay
this.createPromise('2', 1000)
];
var groups = [group1, group2];
I can easily run a single item from group 1 followed by a single item in group 2:
var q = Q();
q = q.then(group1[0]);
q = q.then(group2[0]);
Output:
Executing A
A resolved
Executing 1
1 resolved
But I simply cannot wrap my head around how to combine all items in group 1 into a single promise.
For example, in this case group1 is not run at all - only group2's first item is executed.
var q = Q();
q = q.then(Q.all(group1));
q = q.then(group2[0]);