首页 > 解决方案 > 在承诺解决之前表达 JS 退出 API

问题描述

在映射中,我有两个将进入的对象defaultswitch1 个将进入ORDER_OPENcase 的记录,并且该对象不会进入 if 语句,只是它会推送到,orderArray但是当 API 执行时,我只从default何时接收两个对象我记录了它在执行 API 后orderArray正在推送。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);
 });

标签: javascriptnode.jsexpressasynchronouspromise

解决方案


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

正确的方法,只是等到所有工作完成:

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

推荐阅读