Promise All 在第一个 Promise 之后卡住了
Promise All stuck after first Promise
我有 5 个承诺等待使用 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));
代码只运行了第一个promise,然后卡住了。
为什么不继续下一个承诺 (2-3-4-5) ?
编辑:
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"
});
}
});
};
};
! 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 调用和 await
它,或者在 MongoDB 的情况下,您需要做的就是放弃回调以取回承诺。使 GenerateWeekWorkPlan
本身成为 async
函数,以便它 returns 成为一个承诺:
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"
};
}
}
我有 5 个承诺等待使用 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));
代码只运行了第一个promise,然后卡住了。 为什么不继续下一个承诺 (2-3-4-5) ?
编辑:
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"
});
}
});
};
};
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 调用和 await
它,或者在 MongoDB 的情况下,您需要做的就是放弃回调以取回承诺。使 GenerateWeekWorkPlan
本身成为 async
函数,以便它 returns 成为一个承诺:
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"
};
}
}