原生ajax 与 fetch 请求

项目需求:循环请求一个接口(最多请求30次,最少请求1次),把每次请求回得数据放到一个数组,最后得到得数组进行页面渲染。
项目方案:Promise(Promise.all),ajax
项目问题: 1、ajax同步请求,当请求次数过多时,线程堵塞,造成接口返回慢。
2、ajax异步请求,所有数据返回时间不确定。

解决方案:把ajax请求换成fetch请求

for (var i = 0; i < days; i = i + 2) {
	let arry = []
    arry.push(
      fetch(路径地址, {
          method:'GET',
          headers: token,
          }).then((res)=>res.json())
    )
} 

最后得到得arry

Promise.all(
    arry//你需要的那个数组
).then(
    (res) => {
        //成功
).catch(err => {
//失败
    reject(err)
})

你可能感兴趣的:(原生ajax 与 fetch 请求)