Skip to content

Instantly share code, notes, and snippets.

@thestephenmarshall
Last active October 29, 2015 15:58
Show Gist options
  • Save thestephenmarshall/d4e85e4807c91b5eb816 to your computer and use it in GitHub Desktop.
Save thestephenmarshall/d4e85e4807c91b5eb816 to your computer and use it in GitHub Desktop.
Wait for loop to complete using async/await
async () => {
let done = () => {
return new Promise((resolve, reject) => {
async () => {
let completed = 0;
const list = [0,1,2];
for(let x in list) {
let tbd = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true);
}, 1000)
});
};
let t = await tbd();
console.log(`awaited t[${x}] = ${t}`);
if(++completed === list.length) resolve();
}
}();
});
};
await done();
console.log('All done!')
}();
console.log('Let\'s see what happens..');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment