Javascript fetch
函数异步地从指定的url
中提取资源。同时fetch
返回Promise
。 Promise
可以帮助执行异步部分,并在资源以获取的资源作为参数加载后运行传入then
(res => res.json()
)的函数。如果获取的资源是JSON格式,则可以使用json()
进行解析。
then
还返回Promise
使其可链接。
fetch(url) // asynchronously load contents of the url
// return a Promise that resolves when res is loaded
.then(res => res.json()) // call this function when res is loaded
// return a Promise with result of above function
.then(res => { // call this function when the above chained Promise resolves
this.setState({
data: res,
error: res.error || null,
loading: false
});
res => res.json()
也可以写为(but not exactly equal)
function(res) { return res.json()}
这可怎么更新?它没有显示任何努力。 OP甚至不知道'=>'是什么。这可以简单地通过查看SO和文档来解决 – Weedoze