let shouldHavePaid = 0;
demographicsArray.map((country) => {
if (country.checked == true) {
Price.findOne({ country: country._id }).then((priceRes) => {
if (priceRes) {
shouldHavePaid = shouldHavePaid + priceRes.priceSMS * country.count;
} else {
shouldHavePaid = shouldHavePaid + 0.1 * country.count; //Default price for unlisted countries
}
});
}
});
console.log(`Finish: ${shouldHavePaid}`);
I want the console.log at the end to execute after the map, but it fires before the map is finished. I am expecting this output because as far as I know map should be sync and not async. I believe that the request to the DB messes it up? what would you suggest here?