2020-08-04 15:13:35 +08:00
---
id: 5cdafbe72913098997531682
2021-02-06 04:42:36 +00:00
title: Handle a Rejected Promise with catch
2020-08-04 15:13:35 +08:00
challengeType: 1
forumTopicId: 301204
2021-01-13 03:31:00 +01:00
dashedName: handle-a-rejected-promise-with-catch
2020-08-04 15:13:35 +08:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
`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:
2020-08-04 15:13:35 +08:00
```js
myPromise.catch(error => {
// do something with the error.
});
```
2021-02-06 04:42:36 +00:00
`error` is the argument passed in to the `reject` method.
2020-08-04 15:13:35 +08:00
2020-12-16 00:37:30 -07:00
# --instructions--
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
Add the `catch` method to your promise. Use `error` as the parameter of its callback function and log `error` to the console.
2020-08-04 15:13:35 +08:00
2020-12-16 00:37:30 -07:00
# --hints--
2020-08-04 15:13:35 +08:00
2021-02-06 04:42:36 +00:00
You should call the `catch` method on the promise.
2020-08-04 15:13:35 +08:00
```js
2021-02-06 04:42:36 +00:00
assert(
__helpers.removeWhiteSpace(code).match(/(makeServerRequest|\))\.catch\(/g)
);
2020-08-04 15:13:35 +08:00
```
2021-02-06 04:42:36 +00:00
Your `catch` method should have a callback function with `error` as its parameter.
2020-08-04 15:13:35 +08:00
```js
2020-12-16 00:37:30 -07:00
assert(errorIsParameter);
2020-08-04 15:13:35 +08:00
```
2021-02-06 04:42:36 +00:00
You should log `error` to the console.
2020-08-04 15:13:35 +08:00
```js
2020-12-16 00:37:30 -07:00
assert(
errorIsParameter & &
2021-02-06 04:42:36 +00:00
__helpers
.removeWhiteSpace(code)
.match(/\.catch\(.*?error.*?console.log\(error\).*?\)/)
2020-12-16 00:37:30 -07:00
);
2020-08-04 15:13:35 +08:00
```
2021-01-13 03:31:00 +01:00
# --seed--
## --after-user-code--
```js
const errorIsParameter = /\.catch\((function\(error\){|error|\(error\)=>)/.test(__helpers.removeWhiteSpace(code));
```
## --seed-contents--
```js
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer is set to false to represent an unsuccessful response from a server
let responseFromServer = false;
2021-02-06 04:42:36 +00:00
2021-01-13 03:31:00 +01:00
if(responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
makeServerRequest.then(result => {
console.log(result);
});
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer is set to false to represent an unsuccessful response from a server
let responseFromServer = false;
2021-02-06 04:42:36 +00:00
2021-01-13 03:31:00 +01:00
if(responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
makeServerRequest.then(result => {
console.log(result);
});
makeServerRequest.catch(error => {
console.log(error);
});
```