【发布时间】:2018-09-23 11:19:24
【问题描述】:
我做了一个函数,它实际上有很多异步调用
函数是这样的
const createOrUpdatePlan = (billPlans, serviceId, update) => {
let billPlansWithId;
let promises = [];
if (!update) {
billPlans.map(bp => {
bp.serviceId = serviceId;
return bp;
});
console.log(billPlans);
return db.serviceBillPlans.bulkCreate(billPlans);
} else {
//first promise
let findPromise = db.coachingClasses
.findAll({
attributes: ['billPlans'],
where: {
id: serviceId
}
})
.then(previousBillPlans => {
//creating new bill plans in edit class
let newBillPlans = billPlans.filter(bp => !bp.id);
if (newBillPlans.length > 0) {
newBillPlans = newBillPlans.map(bp => {
bp.serviceId = serviceId;
return bp;
});
// console.log(newBillPlans);
//second promise
let createPromise = db.serviceBillPlans
.bulkCreate(newBillPlans)
.then(newPlans => {
let p1;
billPlansWithId = billPlans.filter(bp => bp.id);
if (newPlans) {
newPlans.forEach(element => {
let object = {};
object.id = element.id;
(object.name = element.name),
(object.cycle = element.cycle),
(object.fees = element.fees);
billPlansWithId.push(object);
});
}
console.log(billPlansWithId);
billPlans = billPlansWithId;
return billPlans;
});
promises.push(createPromise);
}
});
promises.push(findPromise);
return Promise.all(promises).then((arr) => arr[1] );
}
};
我在另一个函数中调用这个函数,在这个函数调用之后,我正在更新另一个表中的数据,该表由 这个函数
目前createOrUpdatePlan函数第一个promise正在运行,但之后在我插入数据的第二个promise中发生了什么,然后在then
.then(newPlans => {
let p1;
billPlansWithId = billPlans.filter(bp => bp.id);
if (newPlans) {
newPlans.forEach(element => {
let object = {};
object.id = element.id;
(object.name = element.name),
(object.cycle = element.cycle),
(object.fees = element.fees);
billPlansWithId.push(object);
});
}
console.log(billPlansWithId);
billPlans = billPlansWithId;
return billPlans;
});
这个then块代码在这个函数在另一个函数中返回数据之后运行
因为我已经在 then 块中编写了 console.log,所以我得到了类似这样的日志
INSERT INTO `service_bill_plans` (`id`,`service_id`,`name`,`cycle`,`fees`,`created_at`,`updated_at`) VALUES (NULL,'17','Five Months Plan',5,4000,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP),(NULL,'17','Six Months Plan',6,5000,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);
undefined
(node:8412) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): SequelizeValidationError: notNull Violation: coachingClasses.billPlans cannot be null
(node:8412) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[ { id: 1, name: 'Monthly Plan', cycle: 1, fees: 1000 },
{ id: 2, name: 'Yearly Plan', cycle: 12, fees: 10000 },
{ id: 3, name: 'Two Months Plan', cycle: 2, fees: 1500 },
{ id: 4, name: 'Three Months Plan', cycle: 3, fees: 2500 },
{ id: 5, name: 'Four Months Plan', cycle: 4, fees: 3000 },
{ id: 148, name: 'Five Months Plan', cycle: 5, fees: 4000 },
{ id: 149, name: 'Six Months Plan', cycle: 6, fees: 5000 } ]
由于您可以在此函数中看到数据已插入到表中,但之后在 then 块中它不会在此函数中返回数据之前返回。
我真的被这个承诺链卡住了,无法理解我该怎么做。请给点提示
【问题讨论】:
标签: javascript ecmascript-6 promise es6-promise ecmascript-5