'Different logs for the same global variable (Node.js) [duplicate]

I have this code in my Node.js application.

The log inside forEach gives desired result but the one outside forEach gives an empty array.

let biddedListings = [];
    bids.forEach(async (bid) => {
      const listing = await ListingModel.find({ _id: bid.bidOn });
      biddedListings.push(listing[0]);
      // console.log("bidded", biddedListings);
    });
console.log("bidded", biddedListings);

Whys is it that the same global variable is giving different results? What am I doing wrong here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source