Skip to content

Commit 002618a

Browse files
committed
docs(promise): fix typo
1 parent 8ab3b0a commit 002618a

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

docs/promise.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,22 @@ getJSON("/post/1.json").then(
227227
`Promise.prototype.catch`方法是`.then(null, rejection)`的别名,用于指定发生错误时的回调函数。
228228

229229
```javascript
230-
getJSON("/posts.json").then(function(posts) {
230+
getJSON('/posts.json').then(function(posts) {
231231
// ...
232232
}).catch(function(error) {
233233
// 处理 getJSON 和 前一个回调函数运行时发生的错误
234234
console.log('发生错误!', error);
235235
});
236236
```
237237

238-
上面代码中,`getJSON`方法返回一个Promise对象,如果该对象状态变为`Resolved`,则会调用`then`方法指定的回调函数;如果异步操作抛出错误,状态就会变为`Rejected`,就会调用`catch`方法指定的回调函数,处理这个错误。另外,`then`方法指定的回调函数,如果运行中抛出错误,也会被`catch`方法捕获。
238+
上面代码中,`getJSON`方法返回一个 Promise 对象,如果该对象状态变为`Resolved`,则会调用`then`方法指定的回调函数;如果异步操作抛出错误,状态就会变为`Rejected`,就会调用`catch`方法指定的回调函数,处理这个错误。另外,`then`方法指定的回调函数,如果运行中抛出错误,也会被`catch`方法捕获。
239239

240240
```javascript
241-
p.then((val) => console.log("fulfilled:", val))
242-
.catch((err) => console.log("rejected:", err));
241+
p.then((val) => console.log('fulfilled:', val))
242+
.catch((err) => console.log('rejected', err));
243243

244244
// 等同于
245-
p.then((val) => console.log("fulfilled:", val))
245+
p.then((val) => console.log('fulfilled:', val))
246246
.then(null, (err) => console.log("rejected:", err));
247247
```
248248

@@ -297,12 +297,12 @@ promise
297297
// ok
298298
```
299299

300-
上面代码中,Promise在`resolve`语句后面,再抛出错误,不会被捕获,等于没有抛出。
300+
上面代码中,Promise 在`resolve`语句后面,再抛出错误,不会被捕获,等于没有抛出。因为 Promise 的状态一旦改变,就永久保持该状态,不会再变了
301301

302-
Promise对象的错误具有“冒泡”性质,会一直向后传递,直到被捕获为止。也就是说,错误总是会被下一个`catch`语句捕获。
302+
Promise 对象的错误具有“冒泡”性质,会一直向后传递,直到被捕获为止。也就是说,错误总是会被下一个`catch`语句捕获。
303303

304304
```javascript
305-
getJSON("/post/1.json").then(function(post) {
305+
getJSON('/post/1.json').then(function(post) {
306306
return getJSON(post.commentURL);
307307
}).then(function(comments) {
308308
// some code
@@ -519,14 +519,14 @@ var p = Promise.race([p1, p2, p3]);
519519
下面是一个例子,如果指定时间内没有获得结果,就将Promise的状态变为`reject`,否则变为`resolve`
520520

521521
```javascript
522-
var p = Promise.race([
522+
const p = Promise.race([
523523
fetch('/resource-that-may-take-a-while'),
524524
new Promise(function (resolve, reject) {
525525
setTimeout(() => reject(new Error('request timeout')), 5000)
526526
})
527-
])
528-
p.then(response => console.log(response))
529-
p.catch(error => console.log(error))
527+
]);
528+
p.then(response => console.log(response));
529+
p.catch(error => console.log(error));
530530
```
531531

532532
上面代码中,如果5秒之内`fetch`方法无法返回结果,变量`p`的状态就会变为`rejected`,从而触发`catch`方法指定的回调函数。

0 commit comments

Comments
 (0)