-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandle_a_Rejected_Promise_with_catch.js
More file actions
44 lines (32 loc) · 1.03 KB
/
Handle_a_Rejected_Promise_with_catch.js
File metadata and controls
44 lines (32 loc) · 1.03 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
/*
Handle a Rejected Promise with catch
catch is the method used when your promise has been rejected.
It is executed immediately after a promise's reject method is called.Here’s the syntax:
myPromise.catch(error => {
});
error is the argument passed in to the reject method.
EXERCISE
Add the catch method to your promise
Use error as the parameter of its callback function and log error
to the console.
You should call the catch method on the promise.
Your catch method should have a callback function with error as its
parameter.
You should log error to the console.
*/
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer is set to false to represent an
// unsuccessful response from a server
let responseFromServer = false;
if(responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
makeServerRequest.then(result => {
console.log(result);
});
makeServerRequest.catch(error => {
console.log(error);
});