Skip to content

Instantly share code, notes, and snippets.

@bmeck
Created February 10, 2014 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmeck/8910991 to your computer and use it in GitHub Desktop.
Save bmeck/8910991 to your computer and use it in GitHub Desktop.
plug this into regenerator: the transpiled argument to spawn matches the interface to generators 1-1 so it has no issues
// spawn() taken unmodified from https://gist.github.com/Benvie/8327917
// See http://taskjs.org for the original.
function spawn(thunk) {
return new Promise(function(resolve, reject) {
var gen = thunk();
function _next(v) {
return handle(gen.next(v));
}
function _throw(e) {
return handle(gen.throw(e));
}
function handle(result) {
var value = result.value;
if (!result.done && value && typeof value.then === "function") {
return value.then(_next, _throw);
}
return value;
}
Promise.cast(_next()).then(resolve, reject);
});
}
spawn(function* () {
var hello = 'hello';
var result = yield new Promise(function (f,r) {
setTimeout(function () {
f(hello + ' world!');
}, 1e3);
});
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment