Skip to content

Instantly share code, notes, and snippets.

@cballenar
Created October 31, 2021 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cballenar/ec84c2bca0e0c566bdee86c282f98236 to your computer and use it in GitHub Desktop.
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.
/**
* 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