Last active
October 23, 2018 18:04
-
-
Save scottopolis/1308484e52bac4f1c04f4fba37da8ab1 to your computer and use it in GitHub Desktop.
Using a promise inside a for loop
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
// If you want to do a for loop but you are executing a promise inside it, you have to do something like this | |
// It's tricky because you have to wait for the promise to resolve before you go to the next iteration | |
var item = { 1:'$2.00', 3:'$4.00', 5:'$6.00' }; | |
(async function loop() { | |
for ( var id in item ) { | |
await runPromise( id ); | |
} | |
})(); | |
runPromise( id ) { | |
return new Promise( (resolve, reject) => { | |
console.log(id) | |
doSomethingElse().then( ()=> { resolve() } ) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment