Created
October 31, 2021 19:50
-
-
Save cballenar/ec84c2bca0e0c566bdee86c282f98236 to your computer and use it in GitHub Desktop.
Iterate over an array and process each item in a promise with a given delay. Ideal if you need to run items at a set or variable pace. As used in https://github.com/cballenar/propertygrubber to delay items due to timeouts possibly put in place to stop crawlers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Pacer | |
* Iterate over an array and process each item in a promise with a given delay. | |
* Ideal if you need to run items at a set or variable pace. As used in | |
* https://github.com/cballenar/propertygrubber to delay items due to timeouts | |
* possibly put in place to stop crawlers. | |
* | |
* @param {Array} list List of items to process | |
* @param {Number} delay Time in milliseconds to delay each item | |
* @returns {Array} New list of processed items | |
* | |
* Credits | |
* https://medium.com/developer-rants/running-promises-in-a-loop-sequentially-one-by-one-bd803181b283 | |
*/ | |
function pacer(list, delay=1000) { | |
const startTime = Date.now(); | |
const newList = []; | |
function wait(data, ms) { | |
var newData = 'new '+data; | |
console.log(`Waiting: ${ms / 1000} seconds.`); | |
return new Promise((resolve) => { | |
setTimeout(resolve, ms, newData); | |
}); | |
} | |
function paceList(list, i=0) { | |
var item = list[i]; | |
wait(item, delay) | |
.then(newData => { | |
newList.push(newData) | |
console.log(`Received: ${newData}\n`); | |
i++; | |
if (i < list.length) { | |
paceList(list, i); | |
} else { | |
console.log(`Total: ${(Date.now() - startTime) / 1000} seconds.`); | |
return newList; | |
} | |
}); | |
} | |
return paceList(list); | |
} | |
const myData = ['a','b','c','d','e'], | |
myNewData = pacer(myData,1500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment