[英]express JS exit API before promise resolving
in the mapping I have two objects which will go to default
in switch
and 1 record which will go to ORDER_OPEN
case and the object won't enter to if statements and just it will push to orderArray
but when the API is executed I only receive two objects from default
and when I log the orderArray
it is pushing into the objectArray
after the execution of API.在映射中,我有两个对象,它们将在
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) => {...}
is a sync function, it will return a promises array, your code will not wait
until all action in {...}
finish. This is main reason your request only takes small time to response. response.records.map(async (record) => {...}
是一个同步函数,它将返回一个 promises 数组,您的代码不会wait
{...}
所有操作完成。这是您的主要原因请求只需要很少的时间来响应。
Correct way, just wait until all jobs are finish:正确的方法,只需等到所有作业完成:
let promises = response.records.map(async (record) => {...}
await Promise.all(promises); // waiting....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.