Click here to Skip to main content
15,069,133 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a function named getActiveUsers which helps me to find active users

I first want to get data from Reward collection, which will return 100 data to rewards array. After waiting for all the results, I want to go through the retrieved array(rewards) one by one, and get data from another collection call Callback. And this data will be pushed to another array called results one by one.(I have used a map function for that) After awaiting for all 100 records to results array, I want to send it as the response. But unfortunately, my code does not wait for the map function. It send response as a empty array(results). And later runs the map and pushes elements to results array. Please help me fix this by using async/await only. If it is not possible, give me the best solution.

What I have tried:

const getInactiveUsers = async (req, res) => {
let results = [];

const rewards = await Reward.find().distinct('data');

await (reward) => {
    const result = await Callback.findOne({
        data: reward,

return res
    .json({ success: true, count: results.length, data: results })};
Updated 15-Sep-21 22:16pm

1 solution (reward) => ...) returns an array of Promise options.

You can't directly await an array; you need combine the array into a single Promise using Promise.all, and await that instead:
await Promise.all( (reward) => { ... }));

Promise.all() - JavaScript | MDN[^]
Using Promises - JavaScript | MDN[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900