This answer assumes that doSomethingit is an asynchronous operation itself. This means that he will have to give way to the cycle of events and wait for at least one event at a time. If it doSomethingis synchronous, there is no benefit to building it asynchronously.
. (n + 1) (n). , . .
Array Q.all, , doSomething jobs :
return Q.all(jobs.map(doSomething))
.
return jobs.reduce(function (previous, job) {
return previous.then(function () {
return doSomething(job);
});
}, Q());
, , reduceRight .
return jobs.reduceRight(function (next, job) {
return function (previous) {
return doSomething(job).then(function (result) {
if (result.isGood(previous)) return result;
return next(result);
});
});
}, function fallthrough(previous) {
throw new Error("No result was satisfactory");
})();
, , , , reduce .
return functions.reduce(Q.when, Q());
Qs readme , , https://github.com/kriskowal/q#tutorial