Skip to content

Instantly share code, notes, and snippets.

@seanonthenet
Created April 19, 2019 03:45
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 seanonthenet/65e978244f8f89a28cf803cd731a8112 to your computer and use it in GitHub Desktop.
Save seanonthenet/65e978244f8f89a28cf803cd731a8112 to your computer and use it in GitHub Desktop.
Async forEach Pattern
const waitFor = (ms) => new Promise(r => setTimeout(r, ms));
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const start = async () => {
await asyncForEach([1, 2, 3, 4, 5], async (num) => {
await waitFor(1000);
console.log(num);
});
console.log('Done');
}
start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment