Last active
March 29, 2017 11:48
-
-
Save Eli-Goldberg/fb7973b5a7ddbe4f68d97e7189d4fb6b to your computer and use it in GitHub Desktop.
forEach await
This file contains 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
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