-
-
Save rightfold/3cce235388b16e6a13f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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