Skip to content

Instantly share code, notes, and snippets.

@gagan-bansal
Last active June 16, 2022 04:43
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 gagan-bansal/ae4ee2dcf9ae146e892bf0f9840f5a1d to your computer and use it in GitHub Desktop.
Save gagan-bansal/ae4ee2dcf9ae146e892bf0f9840f5a1d to your computer and use it in GitHub Desktop.
Understanding "for await...of"
const arr = []
const dummy = async function (i) {
return new Promise((res, rej) => {
const delay = Math.random() * 1000
setTimeout(() => {
console.log('%s, delayed: %s, timestamp: %s', i, delay, Date.now());
res(i);
}, delay);
});
}
for (let i = 0; i< 10; i++) {
arr.push(dummy(i))
}
(async () => {
const start = Date.now();
for await (const res of arr) {
console.log('%s, timestamp: ', res, Date.now())
}
console.log('Total duration: %s ms', Date.now() - start);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment