多项同步请求,一起返回结果
1. 放在方法里
const nameToCode = async (nameArr) => { const promiseArr = []; for (const itemName of nameArr) { const promise = new Promise((resolve, reject) => { request({ url: '/api/bms/cochlea/materialGroup/searchList', params: { name: itemName }, }).then((thenRes) => { if (thenRes?.code === 'OK' && thenRes?.data?.length > 0) { resolve(thenRes?.data[0].code || ''); } }); }); promiseArr.push(promise); } return Promise.all(promiseArr); };
2. 使用
const res = await nameToCode(['aaa', 'bbb', 'ccc'])
注: 主要使用: for ... of / promise promise.all