Reputation: 21855
I have 5 promises waiting to be executed using Promise.All
GenerateWeekWorkPlan = (.....) => {
return new Promise(async (resolve, reject) => {
// Some logic
// retVal = ... DB Manipulation ...
if (retVal !== null) {
resolve({
status: 200,
msg: "Successfully created ..."
});
} else {
reject({
status: 400,
msg: "Failed to create ..."
});
}
}
}
const promiseWeek1 = this.GenerateWeekWorkPlan(.....);
const promiseWeek2 = this.GenerateWeekWorkPlan(.....);
const promiseWeek3 = this.GenerateWeekWorkPlan(.....);
const promiseWeek4 = this.GenerateWeekWorkPlan(.....);
const promiseWeek5 = this.GenerateWeekWorkPlan(.....);
Promise.all(
promiseWeek1,
promiseWeek2,
promiseWeek3,
promiseWeek4,
promiseWeek5
)
.then(success => console.log(success))
.catch(failed => console.log(failed));
The code runs only the first promise and then get stuck. Why doesn't continue to next promises (2-3-4-5) ?
EDIT:
GenerateWeekWorkPlan = (weekNumber, packageId, arrayOfTargets) => {
return new Promise(async (resolve, reject) => {
// Get all the raw data from Leads collection that is not Duplicated
console.log("GenerateWeekWorkPlan ...");
try {
// 1. Get leads by package id and take all those which are not duplicated
const leads1 = await Leads.find({
Duplicate: false,
PackageId: packageId
})
.lean()
.exec(async (err, leads) => {
// docs are plain javascript objects instead of model instances
const oneWeekWorkPlan = arrayOfTargets.map(target => {
return leads.map(lead => ({
....
}));
});
console.log("--------Insert Many----------");
const flatted = oneWeekWorkPlan.flat();
console.log(flatted);
const retVal = await WeekPlangs.insertMany(flatted);
console.log("--------Done Insert Many----------");
if (retVal !== null) {
console.log("retVal" + retVal);
resolve({
status: 200,
msg: "Successfully created documents"
});
} else {
console.log("Nothing inside retVal" + retVal);
reject({
status: 400,
msg: "Failed to create documents"
});
}
});
} catch (err) {
console.error(err.message);
reject({
status: 400,
msg: "Failed to create documents"
});
}
});
};
};
Upvotes: 0
Views: 809
Reputation: 664307
Never pass an async function
as the executor to new Promise
! It seems you are using it so that you can call resolve
and reject
from the asynchronous exec()
callback - but really you shouldn't have used that callback in the first place. You would want to promisify the call and await
it, or in case of MongoDB all you would need to do is drop the callback to get back a promise. Make GenerateWeekWorkPlan
itself the async
function so that it returns a promise:
async function GenerateWeekWorkPlan(weekNumber, packageId, arrayOfTargets) {
// Get all the raw data from Leads collection that is not Duplicated
console.log("GenerateWeekWorkPlan ...");
try {
// 1. Get leads by package id and take all those which are not duplicated
const leads = await Leads.find({
Duplicate: false,
PackageId: packageId
}).lean();
// docs are plain javascript objects instead of model instances
const oneWeekWorkPlan = arrayOfTargets.flatMap(target => {
return leads.map(lead => ({
…
}));
});
console.log("--------Insert Many----------");
console.log(oneWeekWorkPlan);
const retVal = await WeekPlangs.insertMany(oneWeekWorkPlan);
console.log("--------Done Insert Many----------");
if (retVal !== null) {
console.log("retVal" + retVal);
return {
status: 200,
msg: "Successfully created documents"
};
} else {
console.log("Nothing inside retVal" + retVal);
throw {
status: 400,
msg: "Failed to create documents"
};
}
} catch (err) {
console.error(err.message);
throw {
status: 400,
msg: "Failed to create documents"
};
}
}
Upvotes: 1