繁体   English   中英

在承诺解决之前表达 JS 退出 API

[英]express JS exit API before promise resolving

在映射中,我有两个对象,它们将在switch进入default状态,1 个记录将进入ORDER_OPEN案例,并且该对象不会进入 if 语句,只会推送到orderArray但当 API 执行时,我只收到两个从default对象开始,当我记录orderArray它会在 API 执行后推入objectArray

router.get('/orderByPhone/:id', async (req, res) => {
const { ORDER_OPEN, ORDER_FILL, BITY_FILL, BITY_CANCEL, getOrderStatusValue } = require('../../lib/constants/orderStatus');
const statusUtils = require('../../lib/constants/orderStatus');
const apiUtils = require('../../lib/apiUtils');
const neo4jUtils = require('../../lib/neo4jUtils');
const orderArray = [];

try {
    const id = req.params.id;

    const response = await neo4jUtils.getOrders(1, id);

    response.records.map(async (record) => {
        switch (record._fields[0].properties.orderStatus) {

            case ORDER_OPEN:
                const ret = await apiUtils.fetchOrderStatus(record._fields[0].properties.bityId, record._fields[0].properties.token);

                if (ret.legacy_status == BITY_FILL) {
                    await neo4jUtils.updateOrderStatus(record._fields[0].properties.bityId, getOrderStatusValue(ret.legacy_status))
                } else if (ret.legacy_status == BITY_CANCEL) {
                    await neo4jUtils.updateOrderStatus(record._fields[0].properties.bityId, getOrderStatusValue(ret.legacy_status))
                }
                orderArray.push({
                    input: {
                        amount: ret.input.amount,
                        currency: ret.input.currency
                    },
                    ouput: {
                        amount: ret.output.amount,
                        currency: ret.output.currency
                    },
                    status: {
                        status: statusUtils.getOrderStatusValue(ret.legacy_status)
                    }
                });

                break;
            case ORDER_FILL:
                orderArray.push({
                    input: {
                        amount: record._fields[0].properties.fromAmount,
                        currency: record._fields[0].properties.fromCurrency
                    },
                    ouput: {
                        amount: record._fields[0].properties.toAmount,
                        currency: record._fields[0].properties.toCurrency
                    },
                    status: {
                        status: record._fields[0].properties.orderStatus
                    }
                });
                break;
            default:
                orderArray.push({
                    input: {
                        amount: record._fields[0].properties.fromAmount,
                        currency: record._fields[0].properties.fromCurrency
                    },
                    ouput: {
                        amount: record._fields[0].properties.toAmount,
                        currency: record._fields[0].properties.toCurrency
                    },
                    status: {
                        status: record._fields[0].properties.orderStatus
                    }
                });
                break;
        }
    });

} catch (error) {
    res.status(500).send(errorHandleing.FiveZeroZero)
}
res.status(200).json(orderArray);
 });

response.records.map(async (record) => {...}是一个同步函数,它将返回一个 promises 数组,您的代码不会wait {...}所有操作完成。这是您的主要原因请求只需要很少的时间来响应。

正确的方法,只需等到所有作业完成:

let promises = response.records.map(async (record) => {...}
await Promise.all(promises); // waiting....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM