5
Consider the following function:
function* gerador() {
let foo = yield "a"
let bar = yield "b"
let baz = yield "c"
return `${foo} ${bar} ${baz}`
}
let gen = gerador();
console.log(gen.next());
console.log(gen.next());
console.log(gen.next());
console.log(gen.next());
The output will be as follows::
{ value: 'a', done: false }
{ value: 'b', done: false }
{ value: 'c', done: false }
{ value: 'undefined undefined undefined', done: true }
Notice that the last console.log(gen.next());
returned undefined
for the variables foo
, bar
and baz
.
What I can’t understand is that expression yield "a"
returns the object { value: 'a', done: false }
but the variable foo
remains undefined
and to assign the value, you would have to pass as parameter the value
in the next method next()
:
let gen = gerador();
let v1 = gen.next().value;
let v2 = gen.next(v1).value;
let v3 = gen.next(v2).value;
console.log(gen.next(v3));
So I would have expected a grudge:
{ value: 'a b c', done: true }
Interesting question!
+1
– Sergio