让我先给你看看代码是什么样子的
Cart.getCompleteCart((cart)=>{
let products = []; *//this Array has to be filled*
totalPrice = cart.totalPrice;
for(let prod of cart.products){
Product.getProductDetails(prod.productId, productDetails => {
products.push({
product: productDetails,
productCount: prod.productCount
});
});
}
console.log("Products are ->", products); *//this line is running before for loop!*
}
Run Code Online (Sandbox Code Playgroud)
console.log() 在 for 循环完成其工作之前运行。
如何与 console.log() 同步运行“for 循环”?