0

So I have a function that calls my API:

const trackPurchase = async (customerId, unitsPurchased) => {

    const response = await fetch("http://localhost:5000/purchases", { 
        method: "POST",
        headers: {
            'Content-Type': 'application/json',
            Accept: 'application/json',
          },
        body: JSON.stringify({
            "customerId": customerId,
            "unitsPurchased": unitsPurchased
        })
    })

    return await response.text()
};
module.exports = { trackPurchase };

In another file I call the above function:

const trackPurchases = async () => {
    try {
      const units = await trackPurchase (123, 1)
      return units 
    } catch (error) {
        console.log("ERROR ",error)
    }
};

console.log(
    "trackPurchases ",
    trackPurchases ()
)

Logging the result I get a promise. Meanwhile, the request succeeded and the database is updated. What am I missing? Why isn't the promise resolving?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
b c
  • 53
  • 4
  • 1
    `trackPurchases` is also `async`, thus will result in a promise, which you also need to `await`, yes. – deceze Oct 20 '22 at 06:37

0 Answers0