mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 18:32:22 +00:00
docs(async): edit await
This commit is contained in:
parent
4e13461d83
commit
ecd868e5bc
@ -210,7 +210,7 @@ getTitle('https://tc39.github.io/ecma262/').then(console.log)
|
||||
|
||||
### await 命令
|
||||
|
||||
正常情况下,`await`命令后面是一个 Promise 对象。如果不是,就返回对应的值。
|
||||
正常情况下,`await`命令后面是一个 Promise 对象,返回该对象的结果。如果不是 Promise 对象,就直接返回对应的值。
|
||||
|
||||
```javascript
|
||||
async function f() {
|
||||
@ -225,6 +225,30 @@ f().then(v => console.log(v))
|
||||
|
||||
上面代码中,`await`命令的参数是数值`123`,这时等同于`return 123`。
|
||||
|
||||
另一种情况是,`await`命令后面是一个`thenable`对象(即定义`then`方法的对象),那么`await`会将其等同于 Promise 对象。
|
||||
|
||||
```javascript
|
||||
class Sleep {
|
||||
constructor(timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
then(resolve, reject) {
|
||||
const startTime = Date.now();
|
||||
setTimeout(
|
||||
() => resolve(Date.now() - startTime),
|
||||
this.timeout
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const actualTime = await new Sleep(1000);
|
||||
console.log(actualTime);
|
||||
})();
|
||||
```
|
||||
|
||||
上面代码中,`await`命令后面是一个`Sleep`对象的实例。这个实例不是 Promise 对象,但是因为定义了`then`方法,`await`会将其视为`Promise`处理。
|
||||
|
||||
`await`命令后面的 Promise 对象如果变为`reject`状态,则`reject`的参数会被`catch`方法的回调函数接收到。
|
||||
|
||||
```javascript
|
||||
@ -240,7 +264,7 @@ f()
|
||||
|
||||
注意,上面代码中,`await`语句前面没有`return`,但是`reject`方法的参数依然传入了`catch`方法的回调函数。这里如果在`await`前面加上`return`,效果是一样的。
|
||||
|
||||
只要一个`await`语句后面的 Promise 变为`reject`,那么整个`async`函数都会中断执行。
|
||||
任何一个`await`语句后面的 Promise 对象变为`reject`状态,那么整个`async`函数都会中断执行。
|
||||
|
||||
```javascript
|
||||
async function f() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user