地图内的Js await函数
Js await function inside a map
我有一个按顺序排列的类别 ID 数组,我必须制作一个映射并从这些类别中获取项目,最后保持数组的原始顺序
我一直在做什么:
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数组,但是每次调用这个函数它都会按顺序显示
Promise.all
,不要自带数组:
Promise.all(categoryItem.next_steps.map(next_step =>
categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
)).then(info => {
// ^^^^
res.json({ success: true, info })
});
在使用 Promise.all 时,您不需要创建单独的数组。查看上面的代码,这应该可以工作
const values = await Promise.all(
categoryItem.next_steps.map((next_step) =>
categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
)
);
res.json({ success: true, values });
我有一个按顺序排列的类别 ID 数组,我必须制作一个映射并从这些类别中获取项目,最后保持数组的原始顺序
我一直在做什么:
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数组,但是每次调用这个函数它都会按顺序显示
Promise.all
Promise.all(categoryItem.next_steps.map(next_step =>
categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
)).then(info => {
// ^^^^
res.json({ success: true, info })
});
在使用 Promise.all 时,您不需要创建单独的数组。查看上面的代码,这应该可以工作
const values = await Promise.all(
categoryItem.next_steps.map((next_step) =>
categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
)
);
res.json({ success: true, values });