接上篇https://blog.csdn.net/mushui0633/article/details/106643299,当前端批量发送过来一些订单编号,要求把他们都处理发货的流程,这里使用了递归挨个处理。先上代码:
// 批量发货的递归调用
async function recursive(orderList, userId) {
let reIdList = [];
let tempReIdList = [];
try {
// 调用发货接口
const { body } = await got.post('https://www.处理发货的接口/send_goods', {
json: {
userId: userId,
wxId: orderList[0],
postName: '',
postNo: '',
remarks: ''
},
responseType: 'json'
});
// 如果返回值表示处理正常,记录下来正常的单号
if (body.code) {
reIdList.push(orderList[0]);
}
// 再次回调自己直到数组结束
} catch (rejected) {
// 触发这一句
console.log('error:')
console.log(rejected);
} finally {
if (orderList.shift()) {
if (orderList.length) {
tempReIdList = await recursive(orderList, userId);
}
}
}
return reIdList.concat(tempReIdList);
}
调用递归,传递订单编号的数组和商家的id:
let response = await recursive(orderList, res.wxId);
递归把所有的订单调用了发货流程,并且通过递归把所有正常发货订单编号再次返回,进而可以传递给前端,前端知道哪些正常处理。