小编Ash*_*ari的帖子

如何等待循环完成?

让我先给你看看代码是什么样子的

 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 循环”?

javascript loops asynchronous node.js async-await

1
推荐指数
1
解决办法
1604
查看次数

标签 统计

async-await ×1

asynchronous ×1

javascript ×1

loops ×1

node.js ×1