Run Promise in series.
npm install -S @jswork/next-promise-series
import '@jswork/next-promise-series';
var sleep = (inCallback, inTimeout) => {
var callback = inCallback || nx.noop;
var timeout = inTimeout || 1000;
return new Promise((resolve) => {
setTimeout(() => {
console.log('execute!');
resolve(callback());
}, timeout);
});
};
var p1 = function(value) {
return sleep(() => {
return value + 111;
});
};
var p2 = function(value) {
return sleep(() => {
return value + 222;
});
};
var p3 = function(value) {
return sleep(() => {
return value + 333;
});
};
nx.promiseSeries([p1, p2, p3], 0).then((res) => {
console.log('result:->', res);
});
// Or you can polyfill mode:
Promise.series = nxPromiseSeries;
Code released under the MIT license.