Actually a handy utility function that I call delay()
:
function delay(t, val) { return new Promise(function(resolve) { if (t <= 0) { resolve(val); } else { setTimeout(resolve.bind(null, val), t); } }); }
Then you can use it in the promise chain as follows:
let paramerterArr = ['a','b','c','d','e','f'] parameterArr.reduce(function(promise, item, index) { return promise.then(function(result) {
You can also make a small useful function to do sequential iteration with extra delay:
// delayT is optional (defaults to 0) function iterateSerialAsync(array, delayT, fn) { if (!fn) { fn = delayT; delayT = 0; } array.reduce(function(p, item, index) { return p.then(function() { // no delay on first iteration if (index === 0) delayT = 0; return delay(delayT, item).then(fn) }); }, Promise.resolve()); }
And then you will use it as follows:
iterateSerialAsync(paramerterArr, 50, mySpecialFunction).then(function(finalVal) {
source share