我有一个items
的array
,我想在其中附加一个异步值getProductMinQuantity
。
问题是渲染res.status(200)...
是在编辑item.order_quantity_minimum之前发送的。
我以为下面这样的地图会随着项目的更新而创建一个新的承诺。
newResult
是类型Promise<any>[] | undefined
,我不能执行.then
.catch
,然后在其中执行我的res.status
。
const getCart = async () => {
...
let newResult = result.data?.line_items.physical_items.map(async (item: any) =>
item.order_quantity_minimum = await getProductMinQuantity(item.product_id)
)
res.status(200).json({
data: result.data ? normalizeCart(result.data) : null,
})
}
有没有想过我该怎么安排?
经典问题;不能在同步数组方法(map、forEach、filter等(中使用await
,而是使用for...of
循环。
let newResult = []
for(let item of result.data?.line_items.physical_items) {
item.order_quantity_minimum = await getProductMinQuantity(item.product_id)
newResult.push(item);
}
// Do something with newResult