Wait for nested loops to finish before returning in javascript [duplicate]

I have a list of objects (say staff_comp_array) whose structure is as follows:

staff_comp_array = [{
    "staff_id": "CEB",
    "competency": [{
        "workflow": "Workflow A",
        "task_competency": ["Workflow_A_Task_1", "Workflow_A_Task_2"]
    }, {
        "workflow": "Workflow B",
        "task_competency": ["Workflow_B_Task_1", "Workflow_B_Task_2"]
    }]
},
{
    "staff_id": "XD",
    "competency": [{
        "workflow": "Workflow A",
        "task_competency": ["Workflow_A_Task_3"]
    }, {
        "workflow": "Workflow B",
        "task_competency": ["Workflow_B_Task_2", "Workflow_B_Task_3"]
    }]
}
]

There is a collection in MongoDB which has definitions for each task competency as follows:

{
    "workflow": "Workflow B",
    "tasks": [{
        "task_id": "Workflow_B_Task_1",
        "task_name": "Loading B",
        "task_abbr": "LB"
    }, {
        "task_id": "Workflow_B_Task_2",
        "task_name": "Planning B",
        "task_abbr": "PB"
    },{
        "task_id": "Workflow_B_Task_3",
        "task_name": "Checking B",
        "task_abbr": "CB"
    },{
        "task_id": "Workflow_B_Task_4",
        "task_name": "Review B",
        "task_abbr": "RB"
    }]
},
{
    "workflow": "Workflow A",
    "tasks": [{
        "task_id": "Workflow_A_Task_1",
        "task_name": "Loading A",
        "task_abbr": "LA"
    }, {
        "task_id": "Workflow_A_Task_2",
        "task_name": "Planning A",
        "task_abbr": "PA"
    },{
        "task_id": "Workflow_A_Task_3",
        "task_name": "Checking A",
        "task_abbr": "CA"
    },{
        "task_id": "Workflow_A_Task_4",
        "task_name": "Review A",
        "task_abbr": "RA"
    }]
}

I need to iterate over each of the objects in staff_comp_array and modify the array such that the result will be as follows:

[{
    "staff_id": "CEB",
    "competency": [{
        "workflow": "Workflow A",
        "task_competency": [{
        "task_id": "Workflow_A_Task_1",
        "task_name": "Loading A",
        "task_abbr": "LA"
     },{
        "task_id": "Workflow_A_Task_2",
        "task_name": "Planning A",
        "task_abbr": "PA"
    }]
    }, {
        "workflow": "Workflow B",
        "task_competency": [{
        "task_id": "Workflow_B_Task_1",
        "task_name": "Loading B",
        "task_abbr": "LB"
    },{
        "task_id": "Workflow_B_Task_2",
        "task_name": "Planning B",
        "task_abbr": "PB"
    }]
    }]
},
{
    "staff_id": "XD",
    "competency": [{
        "workflow": "Workflow A",
        "task_competency": [{
        "task_id": "Workflow_A_Task_3",
        "task_name": "Checking A",
        "task_abbr": "CA"
    }]
    }, {
        "workflow": "Workflow B",
        "task_competency": [{
        "task_id": "Workflow_B_Task_2",
        "task_name": "Planning B",
        "task_abbr": "PB"
    }, {
        "task_id": "Workflow_B_Task_3",
        "task_name": "Checking B",
        "task_abbr": "CB"
    }]
    }]
}
]

Sails is used to access the MongoDB.

staff_comp_array is a long list with around 100 staff. I need to do achieve my above expected output properly. How do I do achieve this with javascript?

fn: async function () {
let staff_comp_array = await StaffCompetency.find({});
staff_comp_array.forEach(function(item){ 
          let temp_competency_list = [];
          item.competency.forEach(function(comp){
              let temp_competency = {};
              let tasks = [];
              let workflow_res = await Workflows.findOne({workflow: comp.workflow});
              comp.task_competency.forEach(function(task){
                tasks.push(workflow_res.tasks.filter(obj => obj.task_id === task)[0]);
              });
              temp_competency['workflow'] = comp.workflow;
              temp_competency['task_competency'] = tasks;
              temp_competency_list.push(temp_competency);
        });
        item.competency = temp_competency_list;
});
return staff_comp_array;
}

Any help would be appreciated!

Thanks