Chapter VI: Beyond the for/of loop

What else can iterators do?

This chapter is a quick survey of various other places iterables are used, besides the for/of loop.

Things that are iterables

Things that accept iterables

Spread operators

function* foo() { yield 'a', yield 'b', yield 'c'; }
function bar() { console.log(arguments); }
bar(...foo()); // => { 0: 'a', 1: 'b', 2: 'c' }
function* foo() { yield 1, yield 2, yield 3; }
console.log([...foo()]); // => [ 1, 2, 3 ]

Destructuring

function* foo() { yield 1, yield 2, yield 3; }
const [ x, y, z ] = foo();
console.log(x); // => 1
console.log(y); // => 2
console.log(z); // => 3

Construction of maps and sets

function* foo() { yield 1, yield 2, yield 3; }
const set = new Set(foo());
console.log(set.has(1)); // => true
console.log(set.has(2)); // => true
console.log(set.has(3)); // => true
function* foo() { yield ['a', 1], yield ['b', 2]; }
const map = new Map(foo());
console.log(map.get('a')); // => 1
console.log(map.get('b')); // => 2

Array.from()

function* foo() { yield 1, yield 2, yield 3; }
console.log(Array.from(foo())); // => [ 1, 2, 3 ]

Promise.all()

function* foo() {
  yield Promise.resolve(1);
  yield Promise.resolve(2);
  yield Promise.resolve(3);
}
Promise.all(foo()).then(arr => {
  console.log(arr); // => [ 1, 2, 3 ]
});

Generator delegation

function* foo() { yield 1, yield 2, yield 3; }
function* bar() { yield* foo(); }
const arr = Array.from(bar());
console.log(arr); // => [ 1, 2, 3 ]

Next: Chapter VII: Delegation and recursion →



Copyright © 2016 by Greg Reimer (github, twitter). Submit issues to the GitHub issues page.