簡體   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