Last active
August 29, 2015 13:57
-
-
Save getify/9816833 to your computer and use it in GitHub Desktop.
exploring my confusion of `yield*`
This file contains 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
function* foo(arr){ | |
for (var i=0; i<arr.length; i++) { | |
console.log("foo running: " + i); | |
yield (arr[i] * 2); | |
} | |
} | |
function* bar(arr) { | |
var f = foo(arr); | |
for (var i=0; i<arr.length; i++) { | |
console.log("bar running: " + i); | |
yield* f; | |
} | |
} | |
for (var k of bar([1,2,3])) { | |
console.log("k: " + k); | |
} | |
// What I expected: | |
// bar running: 0 | |
// foo running: 0 | |
// k: 2 | |
// bar running: 1 | |
// foo running: 1 | |
// k: 4 | |
// bar running: 2 | |
// foo running: 2 | |
// k: 6 | |
// What we ACTUALLY get: | |
// bar running: 0 | |
// foo running: 0 | |
// k: 2 | |
// foo running: 1 | |
// k: 4 | |
// foo running: 2 | |
// k: 6 | |
// bar running: 1 | |
// bar running: 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment