Ghost/ghost/promise/lib/sequence.js
kirrg001 b7b4026fd5 Extended sequence utility
no issue

- support promise and none promise tasks
- helpful if you create an array of operations and not all of the operations/tasks are async
- `response instanceof Promise` does not work for all cases e.g. some usages return a transaction/bookshelf chain
2018-10-03 00:17:45 +02:00

27 lines
703 B
JavaScript

const Promise = require('bluebird');
/**
* expects an array of functions returning a promise
*/
function sequence(tasks /* Any Arguments */) {
const args = Array.prototype.slice.call(arguments, 1);
return Promise.reduce(tasks, function (results, task) {
const response = task.apply(this, args);
if (response && response.then) {
return response.then(function (result) {
results.push(result);
return results;
});
} else {
return Promise.resolve().then(() => {
results.push(response);
return results;
});
}
}, []);
}
module.exports = sequence;