2018-10-03 01:06:16 +03:00
|
|
|
const Promise = require('bluebird');
|
2017-12-14 00:20:02 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* expects an array of functions returning a promise
|
|
|
|
*/
|
|
|
|
function sequence(tasks /* Any Arguments */) {
|
2018-10-03 01:06:16 +03:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1);
|
2017-12-14 00:20:02 +03:00
|
|
|
|
|
|
|
return Promise.reduce(tasks, function (results, task) {
|
2018-10-03 01:06:16 +03:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
2017-12-14 00:20:02 +03:00
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = sequence;
|