Skip to content

Instantly share code, notes, and snippets.

@SevInf
Last active August 29, 2015 14:21
Show Gist options
  • Save SevInf/09a1e7d913fa46d13265 to your computer and use it in GitHub Desktop.
Save SevInf/09a1e7d913fa46d13265 to your computer and use it in GitHub Desktop.
Promises + generators
async(function*() {
console.log('start');
yield asyncFunction();
var value = yield asyncFunction();
console.log('value', value);
});
function asyncFunction() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('lalala');
resolve('123');
}, 5000);
});
}
function async(genFunc) {
var gen = genFunc();
function step(value) {
var next = gen.next(value);
var promise = next.value;
return promise
.then((v) => {
if (!next.done) {
step(v);
}
})
.catch((e) => gen.throw(e));
}
return step();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment