2021-06-15 00:49:18 -07:00
---
id: 5cdafbe72913098997531682
2021-07-14 21:02:51 +05:30
title: Manipular uma promessa rejeitada usando o catch
2021-06-15 00:49:18 -07:00
challengeType: 1
forumTopicId: 301204
dashedName: handle-a-rejected-promise-with-catch
---
# --description--
2021-07-30 23:57:21 +09:00
`catch` é o método usado quando a promessa é rejeitada. Ele é executado imediatamente após o método `reject` da promessa ser chamado. Aqui está a sintaxe:
2021-06-15 00:49:18 -07:00
```js
myPromise.catch(error => {
2021-07-09 21:23:54 -07:00
2021-06-15 00:49:18 -07:00
});
```
2021-07-14 21:02:51 +05:30
O parâmetro `error` é o argumento passado para o método `reject` .
2021-06-15 00:49:18 -07:00
# --instructions--
2021-07-14 21:02:51 +05:30
Adicione o método `catch` à sua promessa. Use `error` como parâmetro de sua função de callback e exiba o valor de `error` no console.
2021-06-15 00:49:18 -07:00
# --hints--
2021-07-14 21:02:51 +05:30
Você deve chamar o método `catch` na promessa.
2021-06-15 00:49:18 -07:00
```js
assert(
__helpers.removeWhiteSpace(code).match(/(makeServerRequest|\))\.catch\(/g)
);
```
2021-07-14 21:02:51 +05:30
O método `catch` deve ter uma função de callback com `error` como seu parâmetro.
2021-06-15 00:49:18 -07:00
```js
assert(errorIsParameter);
```
2021-07-14 21:02:51 +05:30
Você deve exibir o valor de `error` no console.
2021-06-15 00:49:18 -07:00
```js
assert(
errorIsParameter & &
__helpers
.removeWhiteSpace(code)
.match(/\.catch\(.*?error.*?console.log\(error\).*?\)/)
);
```
# --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-07-09 21:23:54 -07:00
2021-06-15 00:49:18 -07:00
if(responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
makeServerRequest.then(result => {
console.log(result);
});
```
# --solutions--
```js
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer is set to false to represent an unsuccessful response from a server
let responseFromServer = false;
2021-07-09 21:23:54 -07:00
2021-06-15 00:49:18 -07: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);
});
```