Last active
April 24, 2016 16:23
-
-
Save ernestlv/b94eac140deb1591ec728147b514bbe2 to your computer and use it in GitHub Desktop.
ES6 different generator expressions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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