INF310: Modern JS, Fall 2017 > Data Structures > Generators and Iterators
Generators and Iterators
Generators
  function* generateNumbers() {
    let i = 0;
    while (true) {
      i += 1;
      yield i;
    }
  }

  const numbers = generateNumbers();
  numbers.next() // { value: 0, done: false }
  numbers.next() // { value: 1, done: false }
  numbers.next() // { value: 2, done: false }
Generator pattern

Using a regular closure instead of a state machine.

  function generateNumbers() {
    let i = 0;
    return {
      next() {
        i += 1;
        return { value: i, done: false};
      }
    }
  }
2way channel
  function* generateNumbers() {
    let number = 0;
    let increment = 1;
    while (true) {
      increment = (yield number) || 1;
      number += increment;
    }
  }

  const numbers = generateNumbers();
  numbers.next() // { value: 0, done: false }
  numbers.next(5) // { value: 5, done: false }
  numbers.next(5) // { value: 10, done: false }
Iterators
  const numbers = {
    [Symbol.iterator]() {
      return generateNumbers();
    }
  };

  for (let n of numbers) {
    console.log(n);
  }
Links