ES6 output (output 1) (output 2) (output 3) ()

function* generatorFunction() {
  yield (yield 1)(yield 2)(yield 3)();
}
var iterator = generatorFunction();

// [1, 2, 3]
var iteratedOver = [iterator.next().value, iterator.next().value, iterator.next().value];

I'm not sure how this works.

yielddoesn't return a function reference, so what are parentheses like (yield 2)doing - are they live arrow anonymous functions without bodies? What are they called using a partial application?

Something is missing for me, can someone explain?


Update. I tried three browsers, Chrome 50.0.2661.86, Safari 9.1 (50.0.2661.86), Firefox 44.0.2, all without errors.

ESFiddle also runs it without errors.

Commentary by Babel commentators also makes mistakes.

The source of the question is http://tddbin.com/#?kata=es6/language/generator/send-function , the second kata.

+4
1

, .

, . - Babel.

yield , , (yield 2) do - ? ?

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

:

function* generatorFunction() {
  yield (yield 1)(yield 2)(yield 3)();
}
var test = (a) => {
  console.log(a);
  return (b) => {
    console.log(b);
    return (c) => {
      console.log(c);
      return 4;
    };
  };
};
var iterator = generatorFunction();
iterator.next(); // {value: 1, done: false}
iterator.next(test); // {value: 2, done: false}
iterator.next("a"); // "a" {value: 3, done: false}
iterator.next("b"); // "b" undefined {value: 4, done: false}
iterator.next("d"); // {value: undefined, done: true}

, ? / yield

function* generatorFunction() {
  let fn1 = yield 1;
  let a = yield 2;
  let fn2 = fn1(a);
  let b = yield 3;
  let fn3 = fn2(b);
  let res = fn3();
  let d = yield res;
  return undefined;
}

Babel .

- . ,

function* generatorFunction() {
  let fn1 = yield 1;
  let a = yield 2;
  let b = yield 3;
  // these are no more executed with only 3 `next` calls
  let fn2 = fn1(a);
  let fn3 = fn2(b);
  let res = fn3();
  let d = yield res;
  return undefined;
}
+4

All Articles