Can someone help me resolve this issue about modifying data in a function

I'm half a noob in JavaScript, I didn't go in depth while learning it for some urgent reasons

I have this function that fetches some data from a mongo DB

module.exports.getPendingCourses = (req,res,next) => {
var pendingcourses = [];
User.findById(req.query.userId, (err,user) => {
    if (user) {
        for (const cour of user.pendingCourses ) {
            course.findById(cour.courseId, (err,cours) => {
            if (cours) { 
        return res.send(pendingcourses);


So basically the output is an empty array, which happens because the callback runs asynchronously. Can someone help me fix it ASAP ? please