Created
January 29, 2016 00:09
-
-
Save Garciat/9f6559d5146c0be660b9 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 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