Generators in Es6

Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's next() method is called, the generator function's body is executed until the first yield expression, which specifies the value to be returned from the iterator or, with yield*, delegates to another generator function. The next() method returns an object with a value property containing the yielded value and a done property which indicates whether the generator has yielded its last value as a boolean. Calling the next() method with an argument will resume the generator function execution, replacing the yield statement where execution was paused with the argument from next(). Example
function* idMaker(){
  var index = 0;
  while(index < 3)
    yield index++;
 
  
}

var gen = idMaker();

console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // undefined
Another small example describing yield behavior
function* idMaker(){
  yield 1;
  yield 5;
  yield "Bon"
  
}

var gen = idMaker();

console.log(gen.next().value); // 1
console.log(gen.next().value); // 5
console.log(gen.next().value); // "Bon"
console.log(gen.next().value); // undefined
Now an example showing the object details
function* idMaker(){
  var index = 0;
  while(index < 3)
    yield index++;
}

var gen = idMaker();

console.log(gen.next()); // [object Object] {
  done: false,
  value: 0
}
console.log(gen.next()); // [object Object] {
  done: false,
  value: 1
}

console.log(gen.next()); // 
[object Object] {
  done: false,
  value: 2
}
console.log(gen.next()); // [object Object] {
  done: true,
  value: undefined
}
You can note the done true at the end.

Share this post