mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-28 21:32:20 +00:00
docs(async): fix async #787
This commit is contained in:
parent
52bca44c4c
commit
f6199af2f6
@ -59,7 +59,7 @@ asyncReadFile();
|
|||||||
|
|
||||||
(3)更广的适用性。
|
(3)更广的适用性。
|
||||||
|
|
||||||
`co`模块约定,`yield`命令后面只能是 Thunk 函数或 Promise 对象,而`async`函数的`await`命令后面,可以是 Promise 对象和原始类型的值(数值、字符串和布尔值,但这时等同于同步操作)。
|
`co`模块约定,`yield`命令后面只能是 Thunk 函数或 Promise 对象,而`async`函数的`await`命令后面,可以是 Promise 对象和原始类型的值(数值、字符串和布尔值,但这时会自动转成立即 resolved 的 Promise 对象)。
|
||||||
|
|
||||||
(4)返回值是 Promise。
|
(4)返回值是 Promise。
|
||||||
|
|
||||||
|
@ -251,7 +251,7 @@ getJSON("/post/1.json").then(
|
|||||||
|
|
||||||
## Promise.prototype.catch()
|
## Promise.prototype.catch()
|
||||||
|
|
||||||
`Promise.prototype.catch`方法是`.then(null, rejection)`的别名,用于指定发生错误时的回调函数。
|
`Promise.prototype.catch`方法是`.then(null, rejection)`或`.then(undefined, rejection)`的别名,用于指定发生错误时的回调函数。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
getJSON('/posts.json').then(function(posts) {
|
getJSON('/posts.json').then(function(posts) {
|
||||||
@ -993,7 +993,7 @@ try {
|
|||||||
上面这样的写法就很笨拙了,这时就可以统一用`promise.catch()`捕获所有同步和异步的错误。
|
上面这样的写法就很笨拙了,这时就可以统一用`promise.catch()`捕获所有同步和异步的错误。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Promise.try(database.users.get({id: userId}))
|
Promise.try(() => database.users.get({id: userId}))
|
||||||
.then(...)
|
.then(...)
|
||||||
.catch(...)
|
.catch(...)
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user