-
-
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(); | |
} |
Just curious why it is called spawn... Do it has any similarity with this spawn which loads and executes a new child process..
Thank you @jakearchibald for the showModalDialog replacement. It's really helpful. Glad I was able to find this simple solution.
@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
@Vaishali-0902:
I'd like to answer your question for you, and I'm going to try, but it's hard to know what to say. Are you working in Chrome like you state up front, or are you actually trying to do something in IE? (This makes a huge difference). Also, what version of Chrome/IE (again, big difference)?
Then, maybe you just made a typo, or maybe you just shouldn't be surprised that ES6 features aren't working properly in IE. But either way in your post you said you tried to write
spawn * function()
If I'm to take you at your word, then you're missing a few basic things that would definitely cause issues:
()
that would actually call the spawn function, as inspawn(function *() { } )
function
.You can read more about this on MDN.
But to answer your question, per the ES6 spec, there is (sort of) an alternative to using the
*
syntax, the GeneratorFunction constuctor. It's definitely not ideal though, since you'd have to pass the function body as a string. Really unwieldy, and I'd really recommend you take a look at your syntax before giving this version a shot:I think you'll agree that's not a way to go.
Finally, why are you using
eval()
? Some say it's evil. I say it can be EVIL. But seriously, read this and make sure that you're not misusing eval.