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 |
'use strict'; const p = console.log; // ジェネレータはアロー関数にできないとのこと const gfn = function* (from, to) { while (from <= to) { yield from++; } }; const mainGenerator = () => { const g = gfn(0, 4); const iterator1 = g[Symbol.iterator](); const iterator2 = g[Symbol.iterator](); p(iterator1 === g); // true p(iterator2 === g); // true p(iterator1.next()); // { value: 0, done: false } p(iterator1.next()); // { value: 1, done: false } p(iterator1.next()); // { value: 2, done: false } p(iterator1.next()); // { value: 3, done: false } p(iterator1.next()); // { value: 4, done: false } p(iterator1.next()); // { value: undefined, done: true } p(iterator2.next()); // { value: undefined, done: true } p(iterator2.next()); // { value: undefined, done: true } p(iterator2.next()); // { value: undefined, done: true } p(iterator2.next()); // { value: undefined, done: true } p(iterator2.next()); // { value: undefined, done: true } p(iterator2.next()); // { value: undefined, done: true } }; // module.exports = mainGenerator; mainGenerator(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
true true { value: 0, done: false } { value: 1, done: false } { value: 2, done: false } { value: 3, done: false } { value: 4, done: false } { value: undefined, done: true } { value: undefined, done: true } { value: undefined, done: true } { value: undefined, done: true } { value: undefined, done: true } { value: undefined, done: true } { value: undefined, done: true } |