Skip to content

Instantly share code, notes, and snippets.

@nikola-wd
Created November 15, 2019 18:16
Show Gist options
  • Select an option

  • Save nikola-wd/b83b35d1df97a16eceb22edf8042f263 to your computer and use it in GitHub Desktop.

Select an option

Save nikola-wd/b83b35d1df97a16eceb22edf8042f263 to your computer and use it in GitHub Desktop.
[async loop] Loop with setTimeout #javascript #async
const waitFor = ms => new Promise(r => setTimeout(r, ms));
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
(async function() {
await asyncForEach([1, 2, 3], async num => {
await waitFor(50);
console.log(num);
});
console.log('Done');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment