Skip to content
Advertisement

Resolve promise inside javascript map function

I have gone through several questions and posts regarding how to accomplish this but none have worked. I have tried an array (no pun intended) of possible solutions involving Promise.*, thenable, await and none have worked.

I have an array of payments that I extract from a Prisma db call, the object looks like this:

JavaScript

I am trying to run another database query using the subscription_id under each payment_data for which I built a simple function:

JavaScript

And then I execute it inside a .map like this:

JavaScript

The problem is no matter what I have tried this never resolves. If I console log mapped_payments the subscription object shows as Promise {<pending>} and entirely empty when I return the response in express.

I am sure this is a simple solution but I have moved all possible solutions (Promise.*, thenable, await) into the function, into the .map and into the res.send but none work.

Any help is appreciated!

Advertisement

Answer

You need to use async map callback to await the get_subscription function, Also it would be better to use Promise.all to handle all mapped promises.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement