Skip to content

Instantly share code, notes, and snippets.

@gurdasnijor
Last active October 21, 2016 07:06
Show Gist options
  • Save gurdasnijor/f0a0180edcec41257f55ff952106b6e6 to your computer and use it in GitHub Desktop.
Save gurdasnijor/f0a0180edcec41257f55ff952106b6e6 to your computer and use it in GitHub Desktop.
co in 16 lines
function co(genFunc) {
let genObj = genFunc();
run();
function run(promiseResult = undefined) {
let item = genObj.next(promiseResult);
if (!item.done) {
// A promise was yielded
item.value
.then(result => run(result))
.catch(error => {
genObj.throw(error);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment