【发布时间】:2017-07-15 17:50:48
【问题描述】:
如果我按如下方式使用 Fetch API:
fetch(url)
.then(response => response.json())
.then(console.log)
我知道这会记录前一个“then”的结果(响应数据),但是为什么在这种情况下 console.log 不需要任何参数?
这背后是否有任何技术推理或文档,是否可以使用任何其他内置方法?
【问题讨论】:
-
因为
then接受一个回调,参数是从最后一个 then 返回的参数,所以参数被传递给 console.log。基本上是.then(function(json) { console.log(json) })或.then(json => console.log(json)) -
它与
.then(thing => console.log(thing))相同,除了您不创建匿名函数并且所有参数都为您传递。任何可调用对象都可以这样使用。
标签: javascript es6-promise fetch-api