Commit b7673d55 authored by David Votrubec's avatar David Votrubec

Added comments about co() utility

parent 120d9806
......@@ -8,6 +8,7 @@
*
* IT DOES NOT WORK WITH PROMISES
* because when continuable.value is a promise object, it can not be called as a function
* For production use it is better that you use the co() utility - see https://github.com/tj/co/
*/
module.exports = function run(/*iterator fn*/ generatorFn) {
var iterator = generatorFn(); // [1]
......
......@@ -47,35 +47,40 @@
// simpler version of spawn() from https://tc39.github.io/ecmascript-asyncawait/#desugaring
// much easier to read and reason about
// For production use it is better that you use the co() utility - see https://github.com/tj/co/
module.exports = function spawn(genF, self) {
return new Promise(function(resolve, reject) {
var gen = genF.call(self);
function step(nextF) {
var next;
function step(nextFn) {
var ret;
try {
next = nextF();
} catch(e) {
ret = nextFn();
} catch(err) {
// finished with failure, reject the promise
reject(e);
reject(err);
return;
}
if (next.done) {
if (ret.done) {
// finished with success, resolve the promise
resolve(next.value);
resolve(ret.value);
return;
}
// not finished, chain off the yielded promise and `step` again
Promise.resolve(next.value).then(function(v) {
step(function() { return gen.next(v); });
}, function(e) {
step(function() { return gen.throw(e); });
Promise.resolve(ret.value).then(function(value) {
step(function() { return gen.next(value); });
}, function(err) {
step(function() { return gen.throw(err); });
});
}
step(function() { return gen.next(undefined); });
// Start it up
step(function() {
return gen.next(undefined);
});
});
}
\ No newline at end of file
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment