JavaScript
Basic
fetch
React
中的fetch
是相对于index.html
页面的路径,它返回一个可阅读流(Stream)res
,调用res.json()
方法,返回一个Promise
对象,将流异步的转换为json
字符串,这是为什么fetch
有两个.then()
函数的原因。
// 流->Promise->data
fetch('./data.json')
.then(res => res.json())
.then(data => {
dispatch({
type: 'GET',
num: Number(data[4])
})
})