-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTask1.js
224 lines (182 loc) · 5.94 KB
/
Task1.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Let’s break this task down step by step as you requested. We will define the functions in three different ways: callbacks, promises, and async/await. The functions are #(), SendEmail(), Login(), GetData(), and DisplayData(). Each will just log a message to the console, and we’ll add delays to simulate asynchronous behavior, with the most delay added to #().
// We’ll also implement error handling in all three approaches.
// 1. Using Callbacks:
// In this approach, each function will call the next function in its callback, ensuring they run in sequence even though #() takes the longest time.
// Code Using Callbacks:
/*
function #(callback) {
setTimeout(() => {
console.log('#');
callback();
}, 5000); // Simulate delay (5 seconds)
}
function SendEmail(callback) {
setTimeout(() => {
console.log('Send Email');
callback();
}, 1000); // Simulate delay (1 second)
}
function Login(callback) {
setTimeout(() => {
console.log('Login');
callback();
}, 2000); // Simulate delay (2 seconds)
}
function GetData(callback) {
setTimeout(() => {
console.log('Get Data');
callback();
}, 1000); // Simulate delay (1 second)
}
function DisplayData() {
setTimeout(() => {
console.log('Display Data');
}, 1000); // Simulate delay (1 second)
}
// Execute the tasks using callbacks
#(() => {
SendEmail(() => {
Login(() => {
GetData(() => {
DisplayData();
});
});
});
});
*/
// Explanation:
// • Each function is called one after the other in a callback chain.
// • The delay is simulated using setTimeout(), and #() has the most delay (5 seconds).
// • The next function is called only after the current function finishes.
// 2. Using Promises:
// In this approach, each function will return a Promise, and we’ll use .then() to chain the functions together. We’ll handle errors using .catch().
// Code Using Promises:
/*
function #() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('#');
resolve();
}, 5000); // Simulate delay (5 seconds)
});
}
function SendEmail() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Send Email');
resolve();
}, 1000); // Simulate delay (1 second)
});
}
function Login() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Login');
resolve();
}, 2000); // Simulate delay (2 seconds)
});
}
function GetData() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Get Data');
resolve();
}, 1000); // Simulate delay (1 second)
});
}
function DisplayData() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Display Data');
resolve();
}, 1000); // Simulate delay (1 second)
});
}
// Execute the tasks using Promises and chaining
#()
.then(() => SendEmail())
.then(() => Login())
.then(() => GetData())
.then(() => DisplayData())
.catch((error) => console.error("Error caught:", error));
*/
// Explanation:
// • Each function returns a Promise that resolves after the delay.
// • The functions are chained together using .then() to ensure they execute in order.
// • If any function encounters an error, the .catch() will handle it.
// 3. Using Async/Await:
// This approach uses async/await for more readable code, where each function is awaited. We’ll also use try/catch for error handling.
// Code Using Async/Await:
/*
function #() {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('#');
resolve();
}, 5000); // Simulate delay (5 seconds)
});
}
function SendEmail() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Send Email');
resolve();
}, 1000); // Simulate delay (1 second)
});
}
function Login() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Login');
resolve();
}, 2000); // Simulate delay (2 seconds)
});
}
function GetData() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Get Data');
resolve();
}, 1000); // Simulate delay (1 second)
});
}
function DisplayData() {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Display Data');
resolve();
}, 1000); // Simulate delay (1 second)
});
}
// // Execute tasks using async/await with try/catch for error handling
async function executeTasks() {
try {
await #();
await SendEmail();
await Login();
await GetData();
await DisplayData();
} catch (error) {
console.error('Caught an error:', error);
}
}
*/
// // Run the tasks
// executeTasks();
// Explanation:
// • Each function is awaited in sequence.
// • Errors are caught using a global try/catch block in the executeTasks() function.
// • If any function throws an error, the execution stops, and the error is caught.
// Error Handling:
// In each case:
// • Callback approach: Error handling would need to be done inside each callback.
// • Promises approach: .catch() handles any promise rejections.
// • Async/await approach: try/catch handles errors across the entire flow.
// How to Execute:
// 1. Save each code snippet in separate files (e.g., callbacks.js, promises.js, asyncAwait.js).
// 2. Open the terminal and run them using:
// node callbacks.js
// node promises.js
// node asyncAwait.js
// This ensures that you can observe the functions executing in sequence with proper delays and error handling.
// Let me know if you have any further questions!