Last active
November 14, 2018 12:58
-
-
Save magnetikonline/4cf93499f92ad4d74468a62db30394cd to your computer and use it in GitHub Desktop.
JavaScript asynchronous generator and iteration example.
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
'use strict'; | |
async function* delayList() { | |
function delay(timeout) { | |
return new Promise((resolve,reject) => { | |
setTimeout(() => resolve(`dealyed for: ${timeout}ms`),timeout); | |
}); | |
} | |
for (let ms of [100,200,300,400]) { | |
yield await delay(ms); | |
} | |
} | |
async function main() { | |
for await (let item of delayList()) { | |
console.log(item); | |
} | |
} | |
main().then(() => { | |
console.log('done'); | |
}); | |
// output: | |
// dealyed for: 100ms | |
// dealyed for: 200ms | |
// dealyed for: 300ms | |
// dealyed for: 400ms | |
// done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment