ffx*_*sam 10 javascript promise
这两个陈述之间究竟有什么区别?
funcThatReturnsAPromise()
.then(() => { /* success */ })
.catch(() => { /* fail */ });
funcThatReturnsAPromise()
.then(() => { /* success */ }, () => { /* fail */ });
Run Code Online (Sandbox Code Playgroud)
除了.catch(fn)作为捷径之外.then(null, fn),你的例子中的不同之处在于
funcThatReturnsAPromise()
.then(() => { /* success */ })
.catch(() => { /* fail */ });
// is equivalent to
const p1 = funcThatReturnsAPromise()
const p2 = p1.then(() => { /* success */ })
const p3 = p2.catch(() => { /*
executed if p1 is rejected
executed if p2 is rejected
*/ })
Run Code Online (Sandbox Code Playgroud)
而第二个是
funcThatReturnsAPromise()
.then(() => { /* success */ }, () => { /* fail */ });
// equivalent to
const p1 = funcThatReturnsAPromise()
const p2 = p1.then(
() => { /* success */ },
() => { /*
executed if p1 is rejected
(p2 will be actually resolved by the result of this function only when p1 is rejected)
*/ }
);
Run Code Online (Sandbox Code Playgroud)
.catch(foo) 等于 .then(undefined, foo)
但是您的代码示例之间存在差异:
funcThatReturnsAPromise()
.then(() => { /* success case of funcThatReturnsAPromise */ })
.catch(() => { /* both fail case of funcThatReturnsAPromise
and fail case of "then" function */ });
funcThatReturnsAPromise()
.then(() => { /* success case of funcThatReturnsAPromise */ },
() => { /* fail case of funcThatReturnsAPromise */ });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1825 次 |
| 最近记录: |