2019-05-14 14:52:01 -05:00
---
id: 5cdafbe72913098997531682
title: Handle a Rejected Promise with catch
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301204
2019-05-14 14:52:01 -05:00
---
## Description
< section id = 'description' >
< code > catch< / code > is the method used when your promise has been rejected. It is executed immediately after a promise's < code > reject< / code > method is called. Here’ s the syntax:
```js
myPromise.catch(error => {
// do something with the error.
});
```
< code > error< / code > is the argument passed in to the < code > reject< / code > method.
< / section >
## Instructions
< section id = 'instructions' >
2019-07-07 07:43:58 -05:00
Add the < code > catch< / code > method to your promise. Use < code > error< / code > as the parameter of its callback function and log < code > error< / code > to the console.
2019-05-14 14:52:01 -05:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
- text: You should call the < code > catch</ code > method on the promise.
2020-09-17 19:08:01 +05:00
testString: assert(__helpers.removeWhiteSpace(code).match(/(makeServerRequest|\))\.catch\(/g));
2019-05-14 14:52:01 -05:00
- text: Your < code > catch</ code > method should have a callback function with < code > error</ code > as its parameter.
testString: assert(errorIsParameter);
- text: You should log < code > error</ code > to the console.
2020-09-17 19:08:01 +05:00
testString: assert(errorIsParameter && __helpers.removeWhiteSpace(code).match(/\.catch\(.*?error.*?console.log\(error\).*?\)/));
2019-05-14 14:52:01 -05:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
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);
});
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2020-09-17 19:08:01 +05:00
const errorIsParameter = /\.catch\((function\(error\){|error|\(error\)=>)/.test(__helpers.removeWhiteSpace(code));
2019-05-14 14:52:01 -05:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
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);
});
```
< / section >