-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11Promise.js
More file actions
164 lines (130 loc) · 4.08 KB
/
Day11Promise.js
File metadata and controls
164 lines (130 loc) · 4.08 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
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
//! uncomment api vairable then fetch tasks will work.
// const api = 'https://jsonplaceholder.typicode.com/posts'
const api = 'paste the https here'
// Create a promise that resolves with a message after a 2-second timeout and log the message to the console.
const promise = new Promise((resolve) => {
setTimeout(() => {
resolve('Resolved after 2 seconds');
}, 2000);
});
promise.then((message) => {
console.log(message);
});
// Create a promise that rejects with an error message after a 2-second timeout and handle the error using .catch()
const rejectPromise = new Promise((_, reject) => {
setTimeout(() => {
reject('Rejected after 2 seconds');
}, 2000);
});
rejectPromise.then((message) => {
console.log(message);
}).catch((error) => {
console.error(error);
});
// Create a sequence of promises that simulate fetching data from a server. Chain the promises to log messages in a specific order.
function fetchData(message, delay) {
return new Promise((resolve) => {
setTimeout(() => {
console.log(message);
resolve(message);
}, delay);
});
}
fetchData('Fetching user data...', 1000)
.then(() => fetchData('Fetching orders data...', 1000))
.then(() => fetchData('Fetching products data...', 1000))
.then(() => fetchData('All data fetched!', 1000))
.catch((error) => {
console.error('An error occurred:', error);
});
// Write an async function that waits for a promise to resolve and then logs the resolved value.
async function myPromise() {
const promise = new Promise((resolve) => {
setTimeout(() => {
resolve('Solved!');
}, 2000);
});
try {
const result = await promise;
console.log('try and catch:', result);
} catch (error) {
console.error(error);
}
}
myPromise();
// Use Fetch API to get data from a public API and log the response data to the console using promises.
fetch(api)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
// Use Fetch API to get data from a public API and log the response data to the console using async/await.
async function responseAwait() {
try {
const data = await fetch(api)
const dataJson = await data.json();
console.log('Dataaa::', dataJson)
} catch (error) {
console.log(error)
}
}
responseAwait();
// use Promise.all to wait for multiple promises to resolve and then log all their values.
const Promise1 = new Promise((resolve) => {
setTimeout(() => {
resolve("Solve 1")
}, 2000);
})
const Promise2 = new Promise((resolve) => {
setTimeout(() => {
resolve("Solve 2")
}, 2000);
})
const Promise3 = new Promise((resolve) => {
setTimeout(() => {
resolve("Solve 3")
}, 2000);
})
const Promise4 = new Promise((resolve) => {
setTimeout(() => {
resolve("Solve 4")
}, 2000);
})
Promise.all([Promise1, Promise2, Promise3, Promise4])
.then((value) => {
console.log(value)
})
.catch((error) => {
console.error('One of the promises rejected:', error);
});
// user promise.race to log the value of the first promise that resolves among multiple promises.
const promise1 = new Promise((resolve) => {
setTimeout(() => {
resolve('Promise 1 resolved');
}, 3000);
});
const promise2 = new Promise((resolve) => {
setTimeout(() => {
resolve('Promise 2 resolved');
}, 1000);
});
const promise3 = new Promise((resolve) => {
setTimeout(() => {
resolve('Promise 3 resolved');
}, 2000);
});
Promise.race([promise1, promise2, promise3])
.then((value) => {
console.log('First resolved promise:', value); // Logs: 'Promise 2 resolved' because the timeout is 1s
})
.catch((error) => {
console.error('One of the promises rejected:', error);
});