Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Last active April 24, 2016 16:23
Show Gist options
  • Save ernestlv/b94eac140deb1591ec728147b514bbe2 to your computer and use it in GitHub Desktop.
Save ernestlv/b94eac140deb1591ec728147b514bbe2 to your computer and use it in GitHub Desktop.
ES6 different generator expressions
var g1 = function* (){ yield 1; yield 2; yield 3; return 'hola'};
var g2 = function* (){ x = yield* g1(); yield 4; return 'bye' }
// yield: 1,2,3,4 ( x becomes 'hola'), bye, done
//else
var g1 = function* (){ yield 1; yield 2; yield 3; return 'hola'};
var g2 = function* (){ yield* g1() };
// yield: 1,2,3, done
// but
var g1 = function* (){ yield 1; yield 2; yield 3; return 'hola'};
var g2 = function* (){ return yield* g1() };
//yield: 1,2,3, 'hola', done
@ernestlv
Copy link
Author

yield* is an operator that is only available inside generators. It yields all items iterated over by an iterable.

function* yieldAllValuesOf(iterable) {
yield* iterable;
}
The most important use case for yield* is to recursively call a generator (which produces something iterable).

Source: http://exploringjs.com/es6/ch_iteration.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment