我有一个按顺序排列的类别in数组,我必须绘制一张地图并从这些类别中获取项目,最后保持数组的原始顺序。
我一直在做的事
const info = []
Promise.all(categoryItem.next_steps.map((next_step) => {
return categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
.then((result) => {
info.push(result)
})
})).then(() => {
res.json({ success: true, info })
})categoryItem.next_steps是按顺序排列的categoryIds数组,但是每次调用这个函数时,它都是按顺序显示的。
发布于 2022-05-06 14:36:09
在使用Promise.all时,不需要创建单独的数组。查看上面的代码,这应该可以工作。
const values = await Promise.all(
categoryItem.next_steps.map((next_step) =>
categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
)
);
res.json({ success: true, values });发布于 2022-05-06 14:33:47
Promise.all does this out of the box,不要带自己的数组:
Promise.all(categoryItem.next_steps.map(next_step =>
categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
)).then(info => {
// ^^^^
res.json({ success: true, info })
});https://stackoverflow.com/questions/72143165
复制相似问题