Skip to content

Files

Latest commit

65871ca · Feb 22, 2021

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Feb 22, 2021

Section 16: Generators

The for ... of ... Loop

The for ... of ... loop iterate through arrays of data.

// Example 1
const colors = ['red', 'green', 'blue'];

for (let color of colors) {
  console.log(color);
}

// Example 2
const numbers = [1, 2, 3, 4];
let total = 0;

for (let number of numbers) {
  total += number;
}


Create a Generator

A Generator is a function that can be entered and exited multiple times. We can create a generator by adding an asterisk symbol * right after the function keyword or left before the function name.

function* shopping() {
  // stuff on the sidewalk
  // walking down the sidewalk
  // go into the store with cash
  const stuffFromStore = yield 'cash';

  // walking to laundry place
  const cleanClothes = yield 'laundry';

  // walking back home
  return [stuffFromStore, cleanClothes];
}

// stuff in the store
const gen = shopping();
gen.next(); // leaving our house
// walked into the store
// walking up and down the aisles...
// purchase our stuff

gen.next('groceries'); // leaving the store with groceries
gen.next('clean clothes');

Note that the yield let us define the different execution state inside the generator. Each yield will return an object like {value: anyType, done: boolean}.