Skip to content

Commit 36edba3

Browse files
committed
docs(async): async 语法
1 parent b39d0e6 commit 36edba3

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

docs/async.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,21 @@ f()
877877
// hello world
878878
```
879879

880+
另一种方法是`await`后面的Promise对象再跟一个`catch`方面,处理前面可能出现的错误。
881+
882+
```javascript
883+
async function f() {
884+
await Promise.reject('出错了')
885+
.catch(e => console.log(e));
886+
return await Promise.resolve('hello world');
887+
}
888+
889+
f()
890+
.then(v => console.log(v))
891+
// 出错了
892+
// hello world
893+
```
894+
880895
(4)如果`await`后面的异步操作出错,那么等同于`async`函数返回的Promise对象被`reject`
881896

882897
```javascript

0 commit comments

Comments
 (0)