2020-08-04 15:13:35 +08:00
|
|
|
---
|
|
|
|
id: 5cdafbe72913098997531682
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 在 catch 中处理 Promise 失败的情况
|
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--
|
|
|
|
|
|
|
|
当 promise 失败时会调用 `catch` 方法。当 promise 的 `reject` 方法执行时会直接调用。用法如下:
|
2020-08-04 15:13:35 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
myPromise.catch(error => {
|
|
|
|
// do something with the error.
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`error` 是传入 `reject` 方法的参数。
|
2020-08-04 15:13:35 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
**注意:** `then` 和 `catch` 方法可以在 promise 后面链式调用。
|
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
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
给 promise 添加 `catch` 方法。用 `error` 做为回调函数的参数并把 `error` 打印到控制台。
|
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
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
应该在 promise 上调用 `catch` 方法。
|
2020-08-04 15:13:35 +08:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.catch\(/g));
|
2020-08-04 15:13:35 +08:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`catch` 方法应该有一个回调函数,函数参数为`error`。
|
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
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
应该打印`error`到控制台。
|
2020-08-04 15:13:35 +08:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(
|
|
|
|
errorIsParameter &&
|
|
|
|
codeWithoutSpaces.match(/\.catch\(.*?error.*?console.log\(error\).*?\)/)
|
|
|
|
);
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if(responseFromServer) {
|
|
|
|
resolve("We got the data");
|
|
|
|
} else {
|
|
|
|
reject("Data not received");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
makeServerRequest.then(result => {
|
|
|
|
console.log(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
makeServerRequest.catch(error => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
```
|