【发布时间】:2021-01-17 23:39:27
【问题描述】:
案例 1:
fetch('/foo')
.then((res) => console.log(res), err => console.log(err))
案例 2:
fetch('/foo')
.then(res => console.log(res))
.catch(err => console.log(err))
这两种情况有什么区别?
【问题讨论】:
标签: javascript
案例 1:
fetch('/foo')
.then((res) => console.log(res), err => console.log(err))
案例 2:
fetch('/foo')
.then(res => console.log(res))
.catch(err => console.log(err))
这两种情况有什么区别?
【问题讨论】:
标签: javascript
第一个没有链接,第二个没有链接
fetch('/foo')
.then(res => console.log(res))
.catch(err => console.log(err))
正在使用承诺链,.catch 真正的意思是.then(null, handler)
检查this MDN page(在chaining部分下)
...
then的参数是可选的,catch(failureCallback)是then(null, failureCallback)的缩写。您可能会看到这用箭头函数表示:
换句话说,第二个只是简写:
fetch('/foo')
.then(res => console.log(res))
.then(null, err => console.log(err))
所以区别只是使用了链接。
【讨论】: