If you're looking for the answer, check out the details in the official documentation:
promised_functions.reduce(Q.when, Q()).then(function () {
// Execute certain actions if all promises are resolved
});
Scroll down to the "Sequences" part of the documentation. To directly copy it:
In case you have multiple promise-producing functions that need to be executed one after another, you can do it manually:
return foo(initialVal).then(bar).then(baz).then(qux);
But, if you wish to run a dynamically created sequence of functions, this is what you need:
var funcs = [foo, bar, baz, qux];
var result = Q(initialVal);
funcs.forEach(function (f) {
result = result.then(f);
});
return result;
You can simplify this using reduce:
return funcs.reduce(function (soFar, f) {
return soFar.then(f);
}, Q(initialVal));
Alternatively, you could opt for the concise version:
return funcs.reduce(Q.when, Q());
That's the information straight from the source.