Skip to content

Instantly share code, notes, and snippets.

@cyan33
Last active November 15, 2017 00:38
Show Gist options
  • Save cyan33/8ba1ffae7159095182429b862a4279f0 to your computer and use it in GitHub Desktop.
Save cyan33/8ba1ffae7159095182429b862a4279f0 to your computer and use it in GitHub Desktop.
function async(makeGenerator) {
return (...args) => {
var generator = makeGenerator.apply(this, args);
function next(result) {
// result => { done: [Boolean], value: [Object] }
if (result.done) return Promise.resolve(result.value);
return Promise.resolve(result.value).then((res) => {
return next(generator.next(res.value));
}).catch((err) => {
return generator.throw(err);
});
}
try {
return next(generator.next());
} catch (ex) {
return Promise.reject(ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment