【问题标题】:express JS exit API before promise resolving在承诺解决之前表达 JS 退出 API
【发布时间】:2019-07-18 18:47:53
【问题描述】:

在映射中,我有两个对象将转到 switch 中的 default 和 1 个记录将转到 ORDER_OPEN 情况,并且该对象不会进入 if 语句,只会推送到 @987654324 @ 但执行 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);
 });

【问题讨论】:

    标签: javascript node.js express asynchronous promise


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 2019-04-13
      • 2019-05-29
      • 2021-08-11
      相关资源
      最近更新 更多