Javascript Function Generator Parameters

I am learning this function from ES6 (function generators) and it is hard for me to understand the following code:

function* HelloGen2() {
  var a = yield 100;
  var b = yield a + 100;

  console.log(b);
}

var gen2 = HelloGen2();

console.log(gen2.next());     // {value: 100, done: false}
console.log(gen2.next(500));  // {value: 600, done: false}
console.log(gen2.next(1000)); // {value: undefined, done: true}

Questions:

1: In the first gen2.next(), a line of code is called var a = yield 100;, is any value set in the variable var a?
 2: In each gen2.next (), only a line until a semicolon is executed?
So, for example, in the second call, the gen2.next(500)line is console.log(b);not executed. 3: I do not understand the last line gen2.next(1000), how does the variable b get the value 1000?

+4
source share
3 answers

, yield next , , , , . , .

 Y = yield X

 Y = next(X)

:

  • X
  • , Y

, (gen2 ) . , next, (null) , . yield 100, 100 . , 100 , 500. , 500 a .. :

    gen                   wait

main next()               null -> pipe
main                      wait

    gen                   pipe -> null
    gen yield 100         100 -> pipe
    gen                   wait

main                      pipe -> arg (100)
console.log(arg)          100
main next(500)            500 -> pipe
main                      wait

    gen a=                pipe -> a (500)
    gen yield a + 100     600 -> pipe
    gen                   wait

main                      pipe -> arg (600)
console.log(arg)          600
main next(1000)           pipe -> 1000
main                      wait

    gen b=                pipe -> b
    console.log(b)        1000
    gen (ended)           done -> pipe

main                      pipe -> arg (done)
console.log(arg)
main (ended)

, , , - yield/next, ( "" "" ) .

  var a = 5

,

 var a = yield 5

= yield. , async, .

+3

yield x generator.next(y) :

  • generator.next(y) {value: x, done: false}
  • yield x y

, . . generator.next() yield ( ).

:

  • var gen2 = HelloGen2() , .
  • gen2.next() yield 100, {value: 100...}.
  • gen2.next(500) , yield 500 ( a) yield a+100, {value: 600...}.
  • gen2.next(1000) , yield 1000 ( b), , {value: undefined, done: true}.

undefined , .

+3

:

  • gen2.next() var a = yield 100;, - var a?

gen2.next(500) 500. undefined.

  1. gen2.next() , ?
    , , gen2.next(500) console.log(b); .

Right The function stops immediately after the last statement yield.

  1. I don’t understand the last line gen2.next(1000), how bdoes the variable get the value 1000?

When called, the next()function continues at the last stop with the help of the operator yield, the value is transferred and assigned b.

It is executed console.log(b), and the function ends at the end, and the generator result is set to { value: undefined, done: true }.

function* HelloGen2() {
    var a, b;
  
    console.log(a, b); // undefined undefined
    a = yield 100;
    console.log(a, b); //       500 undefined
    b = yield a + 100;

    console.log(b);    // 1000
}

var gen2 = HelloGen2();

console.log('#1');
console.log(gen2.next());     // { value: 100, done: false }
console.log('#2');
console.log(gen2.next(500));  // { value: 600, done: false }
console.log('#3');
console.log(gen2.next(1000)); // { value: undefined, done: true }
console.log('#4');
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run codeHide result
+2
source

Source: https://habr.com/ru/post/1694967/


All Articles