-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerators.js
84 lines (63 loc) · 2.12 KB
/
generators.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//==================
// - A Generator function has the ability to stop and then continue later
function* generator(i){
yield i;
yield i+10;
}
// when this function is called it creates a Generator object
const gen = generator(10);
// Object [Generator] {}
console.log(gen);
// to take the first 'yield'
console.log(gen.next().value);
// expected output: 10
console.log("other logic here");
// to take the second 'yield'
console.log(gen.next().value);
console.log(gen.next().value);
// expected output: undefined
//================
// Other example
//==============
const foo = function*() {
yield 'a';
yield 'b';
yield 'c';
};
let str = '';
for (const val of foo()) {
str = str + val;
console.log(str);
console.log("other logic");
}
console.log(str);
/*
==================
INFINITE GENERATOR
====================
With a generator function, values are not evaluated until they are needed.
Therefore a generator allows us to define a potentially infinite data structure.
*/
function* infinite() {
let index = 0;
while (true) {
yield index++;
}
}
const infinitegenerator = infinite(); // "Generator { }"
console.log(infinitegenerator.next().value); // 0
console.log(infinitegenerator.next().value); // 1
console.log(infinitegenerator.next().value); //
//===================
// DIFFERENCE WITH OBSERVABLES
//======================
/*
https://stackoverflow.com/questions/48512319/what-is-the-difference-between-async-generators-and-observables
Judging from the proposed API descriptions:
observables can have multiple subscribers (broadcast), asynchronous iterators can only have a single reader (unicast)
observables push the events, while asynchronous iterators need to be polled
admittedly, the lazy nature of the Observable constructor does blur the lines
--
A Generator function has the ability to stop and then continue later. An Observable can also stop and continue later but you need to subscribe to it first for it to begin.
First Difference - A generator executes when that function is called. An Observable technically only begins to execute or emit values when you subscribe to it.
*/