Instantly share code, notes, and snippets.

Embed
What would you like to do?
function asyncTask(count) {
return new Promise(resolve => {
setTimeout(() => {
resolve(count);
}, 1000);
});
}
const async = function(generator) {
let gen = generator();
function next(value) {
let res = gen.next(value);
if (res.done) {
return;
}
res.value.then((value) => {
next(value)
})
}
next();
}
async(function *() {
let count = 0;
console.log(yield asyncTask(count++));
console.log(yield asyncTask(count++));
console.log("complete");
});
/*
0
1
complete
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment