-
-
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(); | |
} |
@jakearchibald I have this. Although it doesn't handle returning a value, it has some advantages. :-)
function spawn(gen) {
return function(...args) {
let iter = gen(...args);
function step({value, done}) {
if (!done) value.then(v => iter.next(v), e => iter.throw(e)).then(step);
}
step(iter.next());
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @jakearchibald for the showModalDialog replacement. It's really helpful. Glad I was able to find this simple solution.