jskatas.org Continuously Learn JavaScript. Your Way.

Generator: send function to a generator

By calling next() with a function, you can pass it to the generator.

Pass a function to a generator

the generator can receive a function as a value
let fn = function() {}; function* generatorFunction() { assert.equal(yield null, fn); // remember, don't touch this line } let iterator = generatorFunction(); iterator.next(); iterator.next();
pass a function to the iterator, which calls it
function* generatorFunction() { yield (yield 1)(); } var iterator = generatorFunction(); var iteratedOver = [iterator.next().value, iterator.next().value]; assert.deepEqual(iteratedOver, [1, 2]);
nesting yielded function calls
function* generatorFunction() { yield (yield (yield 1)()); } var iteratedOver = []; assert.deepEqual(iteratedOver, [1, 2, 3]);

Required Knowledge

Related Katas


Difficulty Level


First Published

29 June 2015


3 tests to solve