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 |
'use strict'; const p = console.log; const myIterableObject = { [Symbol.iterator]: () => { let cnt = 0; const iterator = { next: () => { const done = 5 <= cnt; return { value: cnt++, done }; }, }; return iterator; }, }; const processIterable = (iterableObject, callback) => { const iterator = iterableObject[Symbol.iterator](); let ir = iterator.next(); while (!ir.done) { callback(ir); ir = iterator.next(); } }; const mainIterator = () => { p(...myIterableObject); p(...myIterableObject); processIterable(myIterableObject, p); processIterable(myIterableObject, p); }; // module.exports = mainIterator; mainIterator(); |
1 2 3 4 5 6 7 8 9 10 11 12 |
0 1 2 3 4 0 1 2 3 4 { value: 0, done: false } { value: 1, done: false } { value: 2, done: false } { value: 3, done: false } { value: 4, done: false } { value: 0, done: false } { value: 1, done: false } { value: 2, done: false } { value: 3, done: false } { value: 4, done: false } |