Skip to content

Instantly share code, notes, and snippets.

@magnetikonline magnetikonline/example.js
Last active Nov 14, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.