Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to use await with map in js

//this will work...
await orderDetails.map((i: any) => {
      orderDetailsRow += `<div class="row">
      <div class="col-sm-8">
        <h2 class="service-title"><a href="">${i.productId}</h2>
        <h5 class="service-subtitle">Qty:${i.qty}</h5>
      </div>
    </div>`;
 });
//this won't
orderDetails.map(async (item) => {
    let itemDetails = await Product.findById(item.productId).select([
          "title",
          "price",
     ]);
  return { itemDetails, qty: item.qty };
});
 
PREVIOUS NEXT
Tagged: #How #await #map #js
ADD COMMENT
Topic
Name
9+6 =