Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active November 14, 2018 12:58
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 magnetikonline/4cf93499f92ad4d74468a62db30394cd to your computer and use it in GitHub Desktop.
Save magnetikonline/4cf93499f92ad4d74468a62db30394cd to your computer and use it in GitHub Desktop.
JavaScript asynchronous generator and iteration example.
'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