for循环异步请求,等所有请求返回了,再继续执行下一步请求

时间:2025-03-28 16:16:48
// urltemp是要发送请求的目标地址数组,长度等于发请求次数 let arr = []; let that = this; for (let i in urlTemp) { arr.push( new Promise(function(resolve, reject) { // 跨域 let realHealthyUrl = window.location.protocol + '//' + window.location.host + '//' + urlTemp[i].replace('http://', '') + '/xxxxx/health'; axios .get(realHealthyUrl) .then(res => { if (!res || !res.data || res.data.statusCode !== 'API-COMMON-INF-OK') { console.log('接口调用失败') } else { resolve(res); } }) .catch(() => { console.log('网站无响应'); }); }) ); } Promise.all(arr).then(result => { this.dosomething(); });