Created
February 10, 2014 05:56
-
-
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
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
// 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