Skip to content

Instantly share code, notes, and snippets.

@Eli-Goldberg
Last active March 29, 2017 11:48
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 Eli-Goldberg/fb7973b5a7ddbe4f68d97e7189d4fb6b to your computer and use it in GitHub Desktop.
Save Eli-Goldberg/fb7973b5a7ddbe4f68d97e7189d4fb6b to your computer and use it in GitHub Desktop.
forEach await
async function waitAndPrint(toPrint) {
await new Promise((resolve, reject) => {
setTimeout(() => {
console.log(toPrint);
resolve();
}, Math.random() * 1000);
});
}
const arrayOfNumbers = [1, 2, 3, 4];
arrayOfNumbers.forEach(async (num) => {
await waitAndPrint(num)
});
console.log('hello !');
// hello !
// 4
// 1
// 2
// 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment