-
-
Save jakearchibald/31b89cba627924972ad6 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 spawn(generatorFunc) { | |
function continuer(verb, arg) { | |
var result; | |
try { | |
result = generator[verb](arg); | |
} catch (err) { | |
return Promise.reject(err); | |
} | |
if (result.done) { | |
return result.value; | |
} else { | |
return Promise.resolve(result.value).then(onFulfilled, onRejected); | |
} | |
} | |
var generator = generatorFunc(); | |
var onFulfilled = continuer.bind(continuer, "next"); | |
var onRejected = continuer.bind(continuer, "throw"); | |
return onFulfilled(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jakearchibald I have this. Although it doesn't handle returning a value, it has some advantages. :-)