Last active
June 16, 2022 04:43
-
-
Save gagan-bansal/ae4ee2dcf9ae146e892bf0f9840f5a1d to your computer and use it in GitHub Desktop.
Understanding "for await...of"
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
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