I have the below code which tries to connect to MongoDB queries for some data and returns the results, but my code trying to print the result before the MongoDB query completes. How can i convert the below code to wait for the query to complete then proceed with printing the results.
Tried researching about Promise, async/await, but this practical example would help to understand the promise, async/await
const getTicks = () => {
TicksModel.find().select({tick:1,_id:0})
.then(results => {
return results;
}).catch( err => {
//TODO - generate alert
console.error(err);
})
}
const ticks = getTicks();
console.log(ticks);
Expected: wait for MongoDB query to complete then console log ticks