1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// 自作のイテラブルなオブジェクトをジェネレータ関数から返してみる 'use strict'; const p = console.log; const getIterableObject = () => { const iterableObject = { [Symbol.iterator]: () => { let cnt = 0; const iterator = { next: () => { if (cnt < 5) { return { value: cnt++, done: false }; } return { value: undefined, done: true }; }, }; return iterator; }, }; return iterableObject; }; const simpleGeneratorFunction = (iterableObject) => { return (function* (iterableObjectParameter) { yield* iterableObjectParameter; })(iterableObject); }; const generator2main = () => { const myIteratorObject1 = getIterableObject(); p(...simpleGeneratorFunction(myIteratorObject1)); p(...simpleGeneratorFunction(myIteratorObject1)); p(...simpleGeneratorFunction(myIteratorObject1)); }; // module.exports = generator2main; generator2main(); |
1 2 3 |
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 |