I have tried to forking in node. For simple calculation it seems to work fine. But when I use for loop, I don't get any result. I have tried using promise also but all I get is promise pending. Below are the code used.
In moduleProgress.js:
const getModules = async (studentArray) => {
let progress = [];
for (const student of studentArray) {
console.log(student); //prints the first student id an then nothing happens
let std = await User.findById(student); //stops here
console.log(std); //nothing
std = {
username: std.username,
firstname: std.firstname,
lastname: std.lastname,
batch: std.batch,
};
progress.push(std);
}
return progress;
};
process.on("message", async (params) => {
const progress = await getModules(params.students);
process.send(progress);
});
In another file:
if (students.length > 0) {
const calculation = fork("helpers/moduleProgress.js");
calculation.send({
students: students,
});
calculation.on("message", (response) => {
allStatus = response;
console.log(response)
});
res.json({
success: true,
allProgress: allStatus,
});