Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created January 29, 2016 00:09
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 Garciat/9f6559d5146c0be660b9 to your computer and use it in GitHub Desktop.
Save Garciat/9f6559d5146c0be660b9 to your computer and use it in GitHub Desktop.
function runAsync(gen) {
return new Promise((resolve, reject) => {
function loop(result) {
if (result.done) {
return resolve(result.value);
}
result.value.then(handleResolved, handleRejected);
}
function handleResolved(value) {
let result;
try {
result = gen.next(value);
} catch (ex) {
reject(ex);
return;
}
loop(result);
}
function handleRejected(error) {
let result;
try {
result = gen.throw(error);
} catch (ex) {
reject(ex);
return;
}
loop(result);
}
loop(gen.next());
});
}
function async(f) {
return function() {
return runAsync(f.apply(this, arguments));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment