const processSaleOutBoundDetailArr = async (saleOutBoundArr, tokenData, accountData) => { let saleOutBoundDetailArr = [] if (saleOutBoundArr?.length > 0) { for (const item of saleOutBoundArr) { const saleOutBoundDetailRes = await getSaleOutBoundDetail( { id: item.id }, { "app-token": tokenData['app-token'], "X-GW-Router-Addr": accountData?.domain } ); if (saleOutBoundDetailRes.data.errcode === 0) { item.detailList = await processMaterial(item.id, saleOutBoundDetailRes.data.data.material_entity, tokenData, accountData) } saleOutBoundDetailArr.push(item) } } return saleOutBoundDetailArr } const processMaterial = async (orderId, materials, tokenData, accountData) => { let materialArr = [] if (materials && materials.length) { const materialIds = [...new Set(materials.map(item => item.material_id))]; const materialPromises = materialIds.map(id => { return getMaterialDetail({ id }, { "app-token": tokenData['app-token'], "X-GW-Router-Addr": accountData?.domain }); }); const materialDetails = await Promise.all(materialPromises); materialArr = materials.map(material => { const materialDetail = materialDetails.find(detail => detail.data.errcode === 0 && detail.data.data.id === material.material_id); if (materialDetail) { const meterailData = materialDetail.data.data; material.model = meterailData.model material.fetch_category_id = meterailData.fetch_category_id material.tax_rate = meterailData.tax_rate material.orderId = meterailData.orderId } return material; }); } return materialArr; }; 优化这段代码
时间: 2024-03-08 10:51:11 浏览: 62
前端大厂最新面试题-var_let_const.docx
这段代码可以优化的地方有:
1. 使用`for...of`循环代替`for`循环,可以使代码更加简洁易读。
2. 使用`Promise.allSettled`代替`Promise.all`,可以避免其中一个Promise被reject而导致整个Promise.all被reject的情况。
3. 将多次调用的相同参数的请求合并为一个请求,可以减少网络请求的次数,提高性能。
4. 使用可选链(?.)和空值合并运算符(??)可以更好地处理可能为空的变量,避免出现错误。
优化后的代码如下:
```javascript
const processSaleOutBoundDetailArr = async (saleOutBoundArr, tokenData, accountData) => {
const saleOutBoundDetailArr = [];
if (saleOutBoundArr?.length > 0) {
for (const item of saleOutBoundArr) {
const saleOutBoundDetailRes = await getSaleOutBoundDetail(
{ id: item.id },
{ "app-token": tokenData['app-token'], "X-GW-Router-Addr": accountData?.domain }
);
if (saleOutBoundDetailRes?.data?.errcode === 0) {
item.detailList = await processMaterial(item.id, saleOutBoundDetailRes.data.data.material_entity, tokenData, accountData);
}
saleOutBoundDetailArr.push(item);
}
}
return saleOutBoundDetailArr;
};
const processMaterial = async (orderId, materials, tokenData, accountData) => {
if (!materials?.length) {
return [];
}
const materialIds = [...new Set(materials.map(item => item.material_id))];
const materialPromises = materialIds.map(id => {
return getMaterialDetail({ id }, { "app-token": tokenData['app-token'], "X-GW-Router-Addr": accountData?.domain });
});
const materialDetails = await Promise.allSettled(materialPromises);
const materialArr = materials.map(material => {
const materialDetail = materialDetails.find(detail => detail.status === "fulfilled" && detail.value?.data?.errcode === 0 && detail.value?.data?.data?.id === material.material_id)?.value;
if (materialDetail) {
const meterailData = materialDetail.data.data;
material.model = meterailData.model;
material.fetch_category_id = meterailData.fetch_category_id;
material.tax_rate = meterailData.tax_rate;
material.orderId = meterailData.orderId;
}
return material;
});
return materialArr;
};
```
阅读全文