Skip to content

Instantly share code, notes, and snippets.

@mightyguava
Last active March 12, 2017 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mightyguava/e479d8a7ada05ecdbbcf2d8afb18d104 to your computer and use it in GitHub Desktop.
Save mightyguava/e479d8a7ada05ecdbbcf2d8afb18d104 to your computer and use it in GitHub Desktop.
Demystifying Async Programming in Javascript - co() simple solution
function co(generator) {
return new Promise((resolve, reject) => {
const g = generator();
function next(nextVal) {
const ret = g.next(nextVal);
if (ret.done) {
return resolve(ret.value);
}
ret.value.then(next);
}
next();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment