consume(generatorFn, opt_self, var_args)code »
Consumes a GeneratorFunction
. Each time the generator yields a
promise, this function will wait for it to be fulfilled before feeding the
fulfilled value back into next
. Likewise, if a yielded promise is
rejected, the rejection error will be passed to throw
.
Example 1: the Fibonacci Sequence.
promise.consume(function* fibonacci() {
var n1 = 1, n2 = 1;
for (var i = 0; i < 4; ++i) {
var tmp = yield n1 + n2;
n1 = n2;
n2 = tmp;
}
return n1 + n2;
}).then(function(result) {
console.log(result); // 13
});
Example 2: a generator that throws.
promise.consume(function* () {
yield promise.delayed(250).then(function() {
throw Error('boom');
});
}).thenCatch(function(e) {
console.log(e.toString()); // Error: boom
});
Parameters
- generatorFn
Function
The generator function to execute.
- opt_self
?Object=
The object to use as "this" when invoking the
initial generator.
- var_args
...*
Any arguments to pass to the initial generator.
Throws
TypeError
If the given function is not a generator.