Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Last active October 23, 2018 18:04
Show Gist options
  • Save scottopolis/1308484e52bac4f1c04f4fba37da8ab1 to your computer and use it in GitHub Desktop.
Save scottopolis/1308484e52bac4f1c04f4fba37da8ab1 to your computer and use it in GitHub Desktop.
Using a promise inside a for loop
// 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