From 8b126e5d767f00ba5f74ac5e4750882d3e17ead5 Mon Sep 17 00:00:00 2001 From: wangsir <1433256766@qq.com> Date: Fri, 2 Aug 2019 14:20:13 +0800 Subject: [PATCH] Update promise.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Promise.prototype.then(),当then中的回调函数返回一个Promise对象时,后面的then中的回调函数不应该写上funcA、funcB这样的方法名,会让人误以为是函数定义,并未调用,造成阅读困难! --- docs/promise.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/promise.md b/docs/promise.md index a48a2cc..27ea878 100644 --- a/docs/promise.md +++ b/docs/promise.md @@ -229,14 +229,14 @@ getJSON("/posts.json").then(function(json) { ```javascript getJSON("/post/1.json").then(function(post) { return getJSON(post.commentURL); -}).then(function funcA(comments) { +}).then(function (comments) { console.log("resolved: ", comments); -}, function funcB(err){ +}, function (err){ console.log("rejected: ", err); }); ``` -上面代码中,第一个`then`方法指定的回调函数,返回的是另一个`Promise`对象。这时,第二个`then`方法指定的回调函数,就会等待这个新的`Promise`对象状态发生变化。如果变为`resolved`,就调用`funcA`,如果状态变为`rejected`,就调用`funcB`。 +上面代码中,第一个`then`方法指定的回调函数,返回的是另一个`Promise`对象。这时,第二个`then`方法指定的回调函数,就会等待这个新的`Promise`对象状态发生变化。如果变为`resolved`,就调用第一个回调函数,如果状态变为`rejected`,就调用第二个回调函数。 如果采用箭头函数,上面的代码可以写得更简洁。