多个请求同时发送
将所有的异步请求用自己的方法放到一个数组中,然后使用promis All ( [ ] ) 返回的就是所有的结果
/*
* Function applyProportion一个异步的请求
* 需求:同时拿到三年的数据(params:[2018, 2019, 2020]);
*/
const initData = async (params) => {
//promiseArr中为三个Promise
const promiseArr = params.year.map(item => {
return applyProportion({ year: item });
});
try {
// 同时发送三个请求,结果res为一个Array [res1, res2, res3]
const res = await Promise.all(promiseArr);
// 根据自己的需求用res
} catch (error) {
console.log(error)
}
}