Skip to content

Instantly share code, notes, and snippets.

@tylerpaige
Last active September 21, 2018 20:13
Show Gist options
  • Save tylerpaige/f9e8b3f992ff31d2661c324a4958dfdf to your computer and use it in GitHub Desktop.
Save tylerpaige/f9e8b3f992ff31d2661c324a4958dfdf to your computer and use it in GitHub Desktop.
const delayedReturn = (i) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(i);
resolve(i);
}, 1000);
});
};
async function* stepSerial(count) {
for (var i = 0; i < count; i++) {
yield await delayedReturn(i);
}
}
async function accumulateSerial(count) {
const acc = [];
for await (const step of stepSerial(count)) {
acc.push(step);
}
return acc;
}
async function init() {
const foobar = await accumulateSerial(5);
console.log('foobar', foobar);
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment