ES6 Generators

ES6 Generators

Today I used one of the most exciting new feature in ES6 the so called generators for the first time.

Here is an example:

function *gen(){
  let x=yield (cb) => { cb(5); };
  console.log("x="+x);
  
  let y=yield (cb) => { cb(3); };
  console.log("y="+y);
  
  yield;
  console.log("x+y="+(x+y) );
}


let it = gen();
it.next().value((n) => {
  
  it.next(n).value((n) => {
    it.next(n);
    it.next();
  });
});