|
| 1 | +// function* generator(i) { |
| 2 | +// yield i; |
| 3 | +// yield i + 10; |
| 4 | +// } |
| 5 | + |
| 6 | +// const generator2 = function*(i) { |
| 7 | +// yield i; |
| 8 | +// yield i + 10; |
| 9 | +// }; |
| 10 | + |
| 11 | +// let gen = generator(10); |
| 12 | +// console.log(gen); |
| 13 | +// console.log(gen.next()); |
| 14 | +// console.log(gen.next()); |
| 15 | + |
| 16 | +// function* foo() { |
| 17 | +// let index = 0; |
| 18 | +// while (true) { |
| 19 | +// yield index++; |
| 20 | +// } |
| 21 | +// } |
| 22 | + |
| 23 | +// const bar = foo(); |
| 24 | +// console.log(bar.next()); |
| 25 | +// console.log(bar.next()); |
| 26 | + |
| 27 | +// const foo = function*() { |
| 28 | +// yield 10; |
| 29 | +// yield 20; |
| 30 | +// }; |
| 31 | + |
| 32 | +// const bar = foo(); |
| 33 | +// console.log(bar.next()); |
| 34 | +// console.log(bar.next()); |
| 35 | +// console.log(bar.next()); |
| 36 | + |
| 37 | +// function* foo(i) { |
| 38 | +// yield i; |
| 39 | +// yield i + 1; |
| 40 | +// yield i + 2; |
| 41 | +// } |
| 42 | + |
| 43 | +// const bar = function*(j) { |
| 44 | +// yield j; |
| 45 | +// yield* foo(j); |
| 46 | +// yield j + 10; |
| 47 | +// }; |
| 48 | + |
| 49 | +// const fooBar = bar(10); |
| 50 | + |
| 51 | +// console.log(fooBar.next()); |
| 52 | +// console.log(fooBar.next()); |
| 53 | +// console.log(fooBar.next()); |
| 54 | +// console.log(fooBar.next()); |
| 55 | +// console.log(fooBar.next()); |
| 56 | +// console.log(fooBar.next()); |
| 57 | + |
| 58 | +// function* logGenerator() { |
| 59 | +// console.log(0); |
| 60 | +// console.log(1, yield); |
| 61 | +// console.log(2, yield); |
| 62 | +// console.log(3, yield); |
| 63 | +// } |
| 64 | + |
| 65 | +// const logger = logGenerator(); |
| 66 | + |
| 67 | +// logger.next("peter"); |
| 68 | +// logger.next("click"); |
| 69 | + |
| 70 | +let foo; |
| 71 | +function* bar() { |
| 72 | + console.log("before async"); |
| 73 | + yield new Promise(resolve => |
| 74 | + setTimeout(() => resolve("Happy new year!"), 1000) |
| 75 | + ).then(x => { |
| 76 | + console.log(x); |
| 77 | + foo.next(); |
| 78 | + }); |
| 79 | + console.log("after async"); |
| 80 | +} |
| 81 | + |
| 82 | +foo = bar(); |
| 83 | +foo.next(); |
| 84 | + |
| 85 | +console.log("Happy new year!"); |
0 commit comments