I have a requirement to run an async function synchronously, I have tried the below approach. But the results are still async,
const asyncFunction = async () => {
const count = await testSchema.countDocuments(); //Get total count from mongoDB
console.log("COUNT ", count);
return count;
};
console.log("BEFORE ASYNC FUNCTION");
(async () => {
await asyncFunction();
})();
console.log("AFTER ASYNC FUNCTION");
Output,
BEFORE ASYNC FUNCTION
AFTER ASYNC FUNCTION
COUNT 0
console.log("AFTER ASYNC FUNCTION");up a line it would work.I have a requirement to run an async function synchronously- something has been lost in translation. Either between the person who gave you the requirement and you, or you and SO. I would suggest you go back and clarify what the person who gave you the requirement actually wants (not just the "make async be sync", but what the "why" they want that)