2018-10-03 01:06:16 +03:00
|
|
|
const Promise = require('bluebird');
|
2014-08-17 10:17:23 +04:00
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
/**
|
|
|
|
* expects an array of functions returning a promise
|
|
|
|
*/
|
2016-02-25 10:10:36 +03:00
|
|
|
function sequence(tasks /* Any Arguments */) {
|
2018-10-03 01:06:16 +03:00
|
|
|
const args = Array.prototype.slice.call(arguments, 1);
|
2016-05-19 14:49:22 +03:00
|
|
|
|
2014-08-17 10:17:23 +04: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;
|
|
|
|
});
|
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = sequence;
|