const processSaleOutBoundArr = 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) { saleOutBoundDetailArr.push(saleOutBoundDetailRes.data.data) } } }
时间: 2024-04-27 16:21:58 浏览: 117
根据所提供的代码,processSaleOutBoundArr 是一个异步函数,接受三个参数:saleOutBoundArr,tokenData和accountData。该函数的主要逻辑是遍历 saleOutBoundArr 数组中的每个元素,然后调用 getSaleOutBoundDetail 函数来获取每个元素的详细信息。
如果 getSaleOutBoundDetail 函数的返回结果的 errcode 属性等于 0,则将该返回结果的 data 属性添加到 saleOutBoundDetailArr 数组中。最后,该函数返回 saleOutBoundDetailArr 数组。
因此,调用该函数将返回一个 Promise,该 Promise 将在 saleOutBoundDetailArr 数组被填充完毕后被解析,并将该数组作为其解析值。
相关问题
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; }; 优化这段代码
这段代码可以优化的地方有:
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;
};
```
const processSaleBoundArr = async (saleBoundArr, tokenData, accountData) => { let saleOutBounds = [] if (saleBoundArr?.length > 0) { let customerIds = [], customArr = [] for (const item of saleBoundArr) { customerIds.push(item.customer_id) customerIds = Array.from(new Set(customerIds)); } for (const i of customerIds) { const customerDetailRes = await getCustomerDetail( { id: i }, { "app-token": tokenData['app-token'], "X-GW-Router-Addr": accountData?.domain } ); if (customerDetailRes.data.errcode === 0) { const customerData = customerDetailRes.data.data customArr.push(customerData) } } for (const s of saleBoundArr) { for (const c of customArr) { if (s.customer_id === c.customer_id) { s.account_open_addr = c.account_open_addr s.addr = c.addr s.bank = c.bank s.bank_account = c.bank_account s.taxpayer_no = c.taxpayer_no s.orgName = c.sale_dept_name saleOutBounds.push(s) } } } } return saleOutBounds } 优化代码
以下是对上述代码的优化建议:
1. 对于 `saleBoundArr?.length > 0` 这个条件判断,可以使用 `if (saleBoundArr && saleBoundArr.length)` 来代替,更加简洁明了。
2. 在获取客户详细信息的过程中,可以使用 `Promise.all()` 方法并行获取多个客户的详细信息,避免顺序执行耗费时间。
3. 可以使用 `Array.prototype.find()` 方法代替双重循环查找客户详细信息,提高查找效率。
4. 在获取客户详细信息时,可以使用 `map()` 方法将客户 `id` 数组转换为客户详细信息数组,避免使用多次 `push()` 方法。
下面是优化后的代码示例:
```javascript
const processSaleBoundArr = async (saleBoundArr, tokenData, accountData) => {
let saleOutBounds = [];
if (saleBoundArr && saleBoundArr.length) {
const customerIds = [...new Set(saleBoundArr.map(item => item.customer_id))];
const customerPromises = customerIds.map(id => {
return getCustomerDetail(
{ id },
{ "app-token": tokenData['app-token'], "X-GW-Router-Addr": accountData?.domain }
);
});
const customerDetails = await Promise.all(customerPromises);
saleOutBounds = saleBoundArr.map(saleBound => {
const customerDetail = customerDetails.find(detail => detail.data.errcode === 0 && detail.data.data.customer_id === saleBound.customer_id);
if (customerDetail) {
const customerData = customerDetail.data.data;
saleBound.account_open_addr = customerData.account_open_addr;
saleBound.addr = customerData.addr;
saleBound.bank = customerData.bank;
saleBound.bank_account = customerData.bank_account;
saleBound.taxpayer_no = customerData.taxpayer_no;
saleBound.orgName = customerData.sale_dept_name;
}
return saleBound;
});
}
return saleOutBounds;
};
```
这个优化后的代码使用了一些 JavaScript 的高级语法,如 `Promise.all()` 和 `Array.prototype.find()` 方法,使代码更加简洁高效。
阅读全文