1 回答

TA貢獻1846條經驗 獲得超7個贊
ASYNC / AWAIT
您可以使用和的組合Promise.all
來按預期填充allProductInfo
。
需要注意的ASYNC / AWAIT
是,您只能在 ASYNC 函數內使用 ASYNC 函數。
activeProductBank.map
將迭代您的所有activeProductBank
并返回一個 Promise 數組,然后將其傳遞給 ,Promise.all
然后在列表中的所有 Promise 都解決后解析。
Promise.all
getProductInfo = async (res) => {
? ? const allProductInfo = Promise.all(
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? activeProductBank.map(SKU => bby.products(SKU, { show:'sku,name'}))
? ? ? ? ? ? ? ? ? ? ? ? ? ? )
? ??
? ? res.send(allProductInfo);
};
另一種方法是使用 for..of 循環并使用 Await 調用逐一推送每個 ProductInfo 的響應,如下所示
getProductInfo = async (res) => {
? ? let allProductInfo = [];
? ? for(let sku of allProductInfo) {
? ? ? ? const productInfo = await bby.products(sku, { show:'sku,name'});
? ? ? ? allProductInfo.push(productInfo);
? ? }
? ??
? ? res.send(allProductInfo);
};
添加回答
舉報