-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day11.js
156 lines (128 loc) · 5.03 KB
/
Day11.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
// Day 11: Promises and Async/Await
// Activity 1: Understanding Promises
// . Task 1: 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,reject) => {
setTimeout( () =>{
resolve('Promise resolved after 2 seconds');
})
})
promise.then((message) => {
console.log(message);
})
// . Task 2: Create a promise that rejects with an error message after a 2-second timeout and handle the error using .catch() .
const promise2 = new Promise((resolve,reject) => {
setTimeout( () =>{
reject('Promise rejected after 2 seconds');
})
})
promise2.catch((message) => {
console.log(message);
})
// Activity 2: Chaining Promises
// . Task 3: Create a sequence of promises that simulate fetching data from a server. Chain the promises to log messages in a specific order.
const promise3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('First promise resolved after 2 seconds');
}, 2000);
});
promise3
.then((result) => {
console.log(result); // Logs the result of the first promise
return new Promise((resolve, reject) => { // Return a new promise
setTimeout(() => {
resolve('Second promise resolved after 1 second');
}, 1000);
});
})
.then((result) => {
console.log(result); // Logs the result of the second promise
return new Promise((resolve, reject) => { // Return another new promise
setTimeout(() => {
resolve('Third promise resolved after 3 seconds');
}, 3000);
});
})
.then((result) => {
console.log(result); // Logs the result of the third promise
});
// Activity 3: Using Async/Await
// . Task 4: Write an async function that waits for a promise to resolve and then logs the resolved value.
async function logResolvedValue() {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Promise resolved value');
}, 2000); // Simulates a task that takes 2 seconds
});
const resolvedValue = await promise; // Wait for the promise to resolve
console.log(resolvedValue); // Log the resolved value
}
logResolvedValue(); // Call the async function
// . Task 5: Write an async function that handles a rejected promise using try-catch and logs the error message.
async function handleRejectedPromise() {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Promise was rejected'));
}, 2000); // Simulates a task that fails after 2 seconds
});
try {
await promise; // Attempt to wait for the promise to resolve
} catch (error) {
console.error(error.message); // Log the error message if the promise is rejected
}
}
handleRejectedPromise(); // Call the async function
// Activity 4: Fetching Data from an API
// . Task 6: Use the fetch API to get data from a public API and log the response data to the console using promises.
const url = 'https://jsonplaceholder.typicode.com/posts/1';
fetch(url)
.then(response => {
if(!response.ok){
throw new Error('Network response was not ok '+response.statusText);
}
return response.json();
})
.then(data=>{
console.log(data);
})
.catch(error =>{
console.error("There was a problem with the fetch",error);
})
// . Task 7: Use the fetch API to get data from a public API and log the response data to the console using async/await.
async function fetchData() {
const apiurl = 'https://jsonplaceholder.typicode.com/posts/2';
try{
const response = await fetch(apiurl);
if(!response.ok){
throw new Error('error occoured',response.statusText);
}
const data = await response.json();
console.log(data);
}
catch(error){
console.log(error);
}
}
fetchData();
// Activity 5: Concurrent Promises
// . Task 8: Use Promise.all to wait for multiple promises to resolve and then log all their values.
const promisea = new Promise((resolve, reject) => resolve('promise1 success'));
const promiseb = new Promise((resolve, reject) => resolve('promise2 success'));
const promisec = new Promise((resolve, reject) => resolve('promise3 success'));
Promise.all([promisea,promiseb,promisec])
.then((result) =>{
console.log('resolved',result);
})
.catch((error)=>{
console.log('rejected',error);
})
// . Task 9: Use Promise.race to log the value of the first promise that resolves among multiple promises.
const promised = new Promise((resolve, reject) => resolve('promise1 success'));
const promisee = new Promise((resolve, reject) => resolve('promise2 success'));
const promisef = new Promise((resolve, reject) => resolve('promise3 success'));
Promise.race([promised, promisee, promisef])
.then((result) => {
console.log('resolved', result); // resolved promise1 success
})
.catch((error) => {
console.log('rejected', error);
});