第一次在map循环中写请求用了async…await,发现请求并没有生效(forEach也是一样),以下是原因和解决方法。
1、原因:
map/forEach内部使用了while姐和callback方式来执行函数,await不会等待callback的执行。
2、解决方法:
(1)使用for循环
const handleRequest = async () => {
const arr = [1,2,3,4];
for (let i = 0; i < arr.length; i++) {
await API.GETRADARPNG().then();//你的请求方法
}
}
(2)使用promise.all()
const handleRequest = async () => {
const resArr = await Promise.all(
arr.map(async (v) => {
await API.GETRADARPNG().then();//你的请求方法
})
);
return resArr;
};
旨在分享~~~~~