2020-08-04 15:13:35 +08:00
|
|
|
---
|
|
|
|
id: 5cdafbd72913098997531681
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 在 then 中处理 Promise 完成的情况
|
2020-08-04 15:13:35 +08:00
|
|
|
challengeType: 1
|
|
|
|
forumTopicId: 301203
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: handle-a-fulfilled-promise-with-then
|
2020-08-04 15:13:35 +08:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
|
|
|
当程序需要花费未知的时间才能完成时 Promise 很有用(比如,一些异步操作),一般是网络请求。网络请求会花费一些时间,当结束时需要根据服务器的响应执行一些操作。这可以用 `then` 方法来实现,当 promise 完成 `resolve` 时会触发 `then` 方法。例子如下:
|
2020-08-04 15:13:35 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
myPromise.then(result => {
|
|
|
|
// do something with the result.
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`result` 即传入 `resolve` 方法的参数。
|
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 添加 `then` 方法。用 `result` 做为回调函数的参数并将 `result` 打印在控制台。
|
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 方法调用 `then` 方法。
|
2020-08-04 15:13:35 +08:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(codeWithoutSpaces.match(/(makeServerRequest|\))\.then\(/g));
|
2020-08-04 15:13:35 +08:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`then` 方法应该有一个回调函数,回调函数参数为 `result`。
|
2020-08-04 15:13:35 +08:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(resultIsParameter);
|
2020-08-04 15:13:35 +08:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
应该打印 `result` 到控制台。
|
2020-08-04 15:13:35 +08:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(
|
|
|
|
resultIsParameter &&
|
|
|
|
codeWithoutSpaces.match(/\.then\(.*?result.*?console.log\(result\).*?\)/)
|
|
|
|
);
|
2020-08-04 15:13:35 +08:00
|
|
|
```
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
const resultIsParameter = /\.then\((function\(result\){|result|\(result\)=>)/.test(__helpers.removeWhiteSpace(code));
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
const makeServerRequest = new Promise((resolve, reject) => {
|
|
|
|
// responseFromServer is set to true to represent a successful response from a server
|
|
|
|
let responseFromServer = true;
|
|
|
|
|
|
|
|
if(responseFromServer) {
|
|
|
|
resolve("We got the data");
|
|
|
|
} else {
|
|
|
|
reject("Data not received");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
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 true to represent a successful response from a server
|
|
|
|
let responseFromServer = true;
|
|
|
|
|
|
|
|
if(responseFromServer) {
|
|
|
|
resolve("We got the data");
|
|
|
|
} else {
|
|
|
|
reject("Data not received");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
makeServerRequest.then(result => {
|
|
|
|
console.log(result);
|
|
|
|
});
|
|
|
|
```
|