-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerators.js
More file actions
91 lines (62 loc) · 1.35 KB
/
Copy pathgenerators.js
File metadata and controls
91 lines (62 loc) · 1.35 KB
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
// JavaScript Generators
// its a special type of function that not returning full result at a time, its produce value one by one
// and able to pause execution
// regular function
function normal(){
return 1
return 2
return 3
}
console.log(normal()); // return 1 and function execution complate
// But Generators function
function* numbers(){
yield 1
yield 2
yield 3
}
// here yield pause execution
for(const number of numbers()){
console.log(number);
}
const generator = numbers()
// console.log(generator.next());
// console.log(generator.next());
// console.log(generator.next());
// console.log(generator.next());
// Generator = Pausable Function
// Normal Function
// start ────── finish
// Generator
// start
// │
// yield
// │
// │ pause
// next()
// │
// yield
// │
// │ pause
// next()
// │
// finish
// Lazy Evaluation
function* numbers() {
console.log("Generating 1");
yield 1;
console.log("Generating 2");
yield 2;
console.log("Generating 3");
yield 3;
}
const generatorX = numbers();
console.log("Created");
console.log(generatorX.next());
// Generator with input
function* test() {
const value = yield "Give me a value";
console.log(value);
}
const generatorY = test();
console.log(generatorY.next());
console.log(generatorY.next(100));