-1

I'm trying to call a await function within a an array.map function.

This is called from frontend to retrieve list from Stripe Products. The stripe product object doesn't return the cost directly it only returns an id which has to be called to return information about the price, the amount.. the currency etc.

    getOneToOneProductDetails = async (req, res, next) => {
        let products = await stripe.products.list({
            limit: 3,
        })

        products = await products.data.map((product) => {
            if(product.active === true)
            return {
                productId: product.id,
                productPrice: product.default_price,
                productDescription: product.description,
                productName: product.name,
                productNumPrice: this.getPrice(product.default_price)

            }
        })
        console.log(products)
        res.send(products)
    }
    getPrice = async(req, res,next) => {
        const result = await stripe.prices.retrieve(
            req
          );
          return result.unit_amount
          console.log(result)
    }

The result I get from the products.data.map is

  {
    productId: 'prod_MdYuD4PT0HjB7Q',
    productPrice: 'price_1LuHn3EIRppAZNR0LNBhUNXN',
    productDescription: 'Single Class - 30 Min',
    productName: 'Trial Lesson',
    productNumPrice: Promise { <pending> }
  },

However the getPrice function works fine and returns the required information... I feel like I am unable to call the await function inside the .map function for some reason. I've searched some similar problems but haven't been able to understand the subject as the examples are pretty different from my situation.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
DMantas
  • 125
  • 2
  • 10
  • 1
    You should be awaiting the getPrice line, but I doubt that is going to work. – epascarello Oct 18 '22 at 20:25
  • Does this answer your question? [Use async await with Array.map](https://stackoverflow.com/questions/40140149/use-async-await-with-array-map) – YK1 Oct 18 '22 at 20:29

1 Answers1

0

You can use async/await syntax with map, but to resolve the promises produced by map you need to use Promise.all

    getOneToOneProductDetails = async (req, res, next) => {
        let products = await stripe.products.list({
            limit: 3,
        })

        products = await Promise.all(products.data.map(async (product) => {
            if(product.active === true)
            return {
                productId: product.id,
                productPrice: product.default_price,
                productDescription: product.description,
                productName: product.name,
                productNumPrice: await this.getPrice(product.default_price)

            }
        }))
        console.log(products)
        res.send(products)
    }

For example

const products = [{
  id: 1,
  default_price: 1,
  name: 'name',
  description: 'description'
}, {
  id: 2,
  default_price: 2,
  name: 'name',
  description: 'description'
}];


(async() => {

  const result = await Promise.all(products.map(async(product) => {
    return {
      productId: product.id,
      productPrice: product.default_price,
      productDescription: product.description,
      productName: product.name,
      productNumPrice: await Promise.resolve(product.default_price)

    }
  }));
  console.log(result)
  
})();
Mina
  • 14,386
  • 3
  • 13
  • 26