Skip to content

Instantly share code, notes, and snippets.

Created March 27, 2014 15:31
  • Star 74 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jakearchibald/31b89cba627924972ad6 to your computer and use it in GitHub Desktop.
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();
Copy link

Just curious why it is called spawn... Do it has any similarity with this spawn which loads and executes a new child process..

Copy link

Thank you @jakearchibald for the showModalDialog replacement. It's really helpful. Glad I was able to find this simple solution.

Copy link

taralx commented Apr 29, 2017

@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 =>, e => iter.throw(e)).then(step);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment