<script>
function * d1 (p) {
p-=1;
yield p;
p-=2;
yield p;
}
var g=d1 (9);
var h;
console.log((h=g.next()).value+','+h.done+';');
console.log((h=g.next()).value+','+h.done+';');
console.log((h=g.next()).value+','+h.done+';');
</script>
gives 8,false; then 6,false; then undefined,true; whereas
<script>
function * d2 (p) {
function * d1 (p) {
p -=1 ;
yield p;
p -=2 ;
yield p;
}
d1(p);
}
var g=d2 (9);
var h;
console.log((h=g.next()).value+','+h.done+';');
console.log((h=g.next()).value+','+h.done+';');
console.log((h=g.next()).value+','+h.done+';');
</script>
gives me three times undefined,true;
As I want the hidden structure of d1 (as inner-function), how can I proceed to still have the same result as on the first sample?
The d2
generator function doesn't yield nor return anything, so you only get undefined.
You probably want to call it passing p
argument, and yield each iterated value with yield*
.
function * d2 (p) {
yield* function * d1 (p) {
p -= 1;
yield p;
p -= 2;
yield p;
}(p);
}