node.js递归调用,递归访问api

接上篇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);

递归把所有的订单调用了发货流程,并且通过递归把所有正常发货订单编号再次返回,进而可以传递给前端,前端知道哪些正常处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值