-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
36 lines (28 loc) · 877 Bytes
/
index.js
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
(function(context) {
function *foo(x) {
yield 1;
yield 2;
yield x;
}
function *bar(x) {
const y = 10 * (yield (x + 2));
const z = yield (y / 5);
return (x + y + z);
}
function demo() {
console.log('\n\nGENERATORS');
const iter = foo(3);
console.log(iter.next()); // { value:1, done:false }
console.log(iter.next()); // { value:2, done:false }
console.log(iter.next()); // { value:3, done:false }
console.log(iter.next()); // { value:undefined, done:true }
for (const v of foo(10)) {
console.log(v);
}
const iterBar = bar(100);
console.log(iterBar.next()); // { value:102, done:false }
console.log(iterBar.next(5)); // { value:10, done:false }
console.log(iterBar.next(30)); // { value:180, done:true }
};
(context || this).demoLibs['generators'] = demo;
})(window);