-
Notifications
You must be signed in to change notification settings - Fork 61
/
Generator.js
161 lines (142 loc) Β· 3.9 KB
/
Generator.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* π‘"JavaScript-with-JC"
π Generator
Generator is a function that can be paused and resumed from where it was paused. It is written as the function keyword followed by an asterisk (*).
Generator returns a Generator object that is used by calling the next method.
π‘ Use cases :-
π 1) Creating Infinite loop ( generating unique Id ).
π 2) Iterators ( looping through an array ).
π 3) Handling promises using async generator.
π 4) State management using Redux Saga.
*/
// simple Example
function simpleExample() {
function* getNumbers() {
yield 1;
yield 2;
yield 3;
}
const numberObject = getNumbers();
const first = numberObject.next();
const second = numberObject.next();
const third = numberObject.next();
const fourth = numberObject.next();
console.log(first); // { value: 1, done: false }
console.log(second); // { value: 2, done: false }
console.log(third); // { value: 3, done: false }
console.log(fourth); // { value: undefined, done: true }
}
// simpleExample();
// Use cases :-
// 1) Creating Infinite loop ( generating unique Id )
function infiniteLoop() {
function* generateId() {
let id = 1;
while (true) {
yield id++;
}
}
const uniqueId = generateId();
console.log(uniqueId.next().value); // 1
console.log(uniqueId.next().value); // 2
console.log(uniqueId.next().value); // 3
console.log(uniqueId.next().value); // 4
console.log(uniqueId.next().value); // 5 => infinite loop
// passing value to next
function passValuetoNext() {
function* generateId() {
let id = 1;
while (true) {
const increment = yield id;
if (increment != null) {
id += increment;
} else {
id++;
}
}
}
const uniqueId = generateId();
console.log(uniqueId.next().value); // 1
console.log(uniqueId.next().value); // 2
console.log(uniqueId.next(5).value); // 7
console.log(uniqueId.next().value); // 8
console.log(uniqueId.next().value); // 9 => infinite loop
}
passValuetoNext();
}
// infiniteLoop()
// 2) Iterators ( looping through an array )
function iterators() {
function* arrayIterator(array) {
for (let i = 0; i < array.length; i++) {
yield array[i];
}
}
const arrayItem = arrayIterator([11, 22, 33, 44]);
console.log(arrayItem.next().value); // 11
console.log(arrayItem.next().value); // 22
console.log(arrayItem.next().value); // 33
console.log(arrayItem.next().value); // 44
}
// iterators();
// 3) Handling promises using async generator
const taskOne = function () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("First task resolved");
}, 1000);
});
};
const taskTwo = function () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Second task resolved");
}, 1000);
});
};
const taskThree = function () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Third task resolved");
}, 1000);
});
};
async function* generateTasks() {
yield await taskOne();
yield await taskTwo();
yield await taskThree();
}
// using then and catch
const tasks = generateTasks();
tasks
.next()
.then((response) => {
console.log(response); // { value: 'First task resolved', done: false }
})
.catch((error) => console.log(error));
tasks
.next()
.then((response) => {
console.log(response); // { value: 'Second task resolved', done: false }
})
.catch((error) => console.log(error));
tasks
.next()
.then((response) => {
console.log(response); // { value: 'Third task resolved', done: false }
})
.catch((error) => console.log(error));
// More cleaner way
async function doTasks() {
const tasks = generateTasks();
for await (let result of tasks) {
console.log(result);
}
}
doTasks().catch((error) => {
console.log("error", error);
});
/* Output
First task resolved
Second task resolved
Third task resolved
*/