Skip to content

Instantly share code, notes, and snippets.

@rightfold
Last active August 29, 2015 14:02
Show Gist options
  • Save rightfold/3cce235388b16e6a13f1 to your computer and use it in GitHub Desktop.
Save rightfold/3cce235388b16e6a13f1 to your computer and use it in GitHub Desktop.
function async(f) {
return function() {
const generator = f.apply(this, arguments);
return new Promise(function(resolve, reject) {
function continue_(value) {
let result;
try {
result = generator.next(value);
} catch (e) {
reject(e);
return;
}
if (result.done) {
resolve(result.value);
} else {
const promise = result.value;
promise.then(function(value) {
continue_(value);
}, function(e) {
generator.throw(e);
});
}
};
continue_(undefined);
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment