在循环中调用接口/异步函数,等到全部返回后再进行下一步操作.
封装接口,用promise返回
let getUrl = (params) => {
return new Promise((resolve) => {
// uploadGetUrl为需要调用的接口
uploadGetUrl(params).then(res => {
resolve(res)
})
})
}
// 用来接收promise返回值
const promiseList = []
imgList.forEach(item => {
const params = {
url: item.src,
name: 'upload-img',
};
promiseList.push(getUrl(params))
})
Promise.all(promiseList).then((res) => {
res.forEach((item, index) => {
// do something....
})
// do something....
});
20240621:张火火