这是我的承诺
findBookings()
.then(Promise.all([findInvoiceSum, findReceipt]))
.then(sendResult)
.catch(err => {
console.log("getBookingList ERR: " + err);
res.json({error:true,err})
}
)
我会怀疑它运行findBookings()并且当它解析时它将使用([findInvoiceSum,findReceipt])运行链,当两者都完成时,它将运行sendResult。
不幸的是,这是从控制台日志中发生的事情
=====START findInvoiceSum=====
=====START findReceipt=====
=====START findBookings=====
=====RESOLVE findInvoiceSum=====
=====RESOLVE findReceipt=====
=====RESOLVE findBookings=====
=====sendResult=====
=====RESOLVE sendResult=====
为什么?为什么我的第一个承诺被忽略了.then?
这是我的控制器:
//
// The FIRST promise that needs to be resolved
//
var findBookings = function() {
return new Promise((resolve, reject) => {
console.log("=====START findBookings=====")
bookingTable.aggregate([
...lots of code...
], function (err, data) {
if (!err) {
bookingArray = data;
console.log("=====RESOLVE findBookings=====")
resolve(data);
} else {
reject(new Error('findBooking ERROR : ' + err));
};
});
})};
//
// The findInvoiceSum chain promise
//
var findInvoiceSum = new Promise(
(resolve, reject) => {
console.log("=====START findInvoiceSum=====")
invoiceSumTable.aggregate([
...lots of code...
], function (err, data) {
if (!err) {
console.log("=====RESOLVE findInvoiceSum=====")
resolve(data);
} else {
reject(new Error('findExpense ERROR : ' + err));
};
});
});
//
// The findReceipt chain promise
//
var findReceipt = new Promise(
(resolve, reject) => {
console.log("=====START findReceipt=====")
receiptTable.aggregate([
...lots of code...
],function(err, data) {
if (!err) {
console.log("=====RESOLVE findReceipt=====")
resolve(data);
} else {
reject(new Error('ERR findPropert : ' + err));
};
});
});
//
// Send the result
//
var sendResult = function([invoiceArray, receiptArray]) {
return new Promise((resolve, reject) => {
console.log("=====sendResult=====")
res.json({error:false,
"booking":bookingArray,
"invoice":invoiceArray,
"receipt":receiptArray})
console.log("=====RESOLVE sendResult=====")
resolve();
});
};
//
// Run the promises
//
findBookings()
.then(Promise.all([findInvoiceSum, findReceipt]))
.then(sendResult)
.catch(err => {
console.log("getBookingList ERR: " + err);
res.json({error:true,err})
}
)
答案 0 :(得分:2)
我会怀疑它运行findBookings(),当它解决了它然后 将使用([findInvoiceSum,findReceipt])
运行链
要完成此任务,您需要传递一个函数
findBookings()
.then(() => Promise.all([findInvoiceSum(), findReceipt()]))
并使findInvoiceSum
和findReceipt
成为一个函数。
function findReceipt() {
return new Promise(
(resolve, reject) => {
console.log("=====START findReceipt=====")
receiptTable.aggregate([
...lots of code...
],function(err, data) {
if (!err) {
console.log("=====RESOLVE findReceipt=====")
resolve(data);
} else {
reject(new Error('ERR findPropert : ' + err));
};
});
});
}
答案 1 :(得分:1)
由于您尚未包裹findInvoiceSum
和findReceipt
函数承诺是自己初始化的,
您可以将promises包装到函数中,并使用Promise.all
,如下所示
var findBookings = function() {
return new Promise((resolve, reject) => {
console.log("=====START findBookings=====")
bookingTable.aggregate([
...lots of code...
], function(err, data) {
if (!err) {
bookingArray = data;
console.log("=====RESOLVE findBookings=====")
resolve(data);
} else {
reject(new Error('findBooking ERROR : ' + err));
};
});
})
};
//
// The findInvoiceSum chain promise
//
var findInvoiceSum = function() {
return new Promise(
(resolve, reject) => {
console.log("=====START findInvoiceSum=====")
invoiceSumTable.aggregate([
...lots of code...
], function(err, data) {
if (!err) {
console.log("=====RESOLVE findInvoiceSum=====")
resolve(data);
} else {
reject(new Error('findExpense ERROR : ' + err));
};
});
});
};
//
// The findReceipt chain promise
//
var findReceipt = function() {
return new Promise(
(resolve, reject) => {
console.log("=====START findReceipt=====")
receiptTable.aggregate([
...lots of code...
], function(err, data) {
if (!err) {
console.log("=====RESOLVE findReceipt=====")
resolve(data);
} else {
reject(new Error('ERR findPropert : ' + err));
};
});
});
};
//
// Send the result
//
var sendResult = function([invoiceArray, receiptArray]) {
return new Promise((resolve, reject) => {
console.log("=====sendResult=====")
res.json({
error: false,
"booking": bookingArray,
"invoice": invoiceArray,
"receipt": receiptArray
})
console.log("=====RESOLVE sendResult=====")
resolve();
});
};
// Find invoicesum and receipt
var invoiceAndReceipt = function() {
return Promise.all([findInvoiceSum(), findReceipt()]).then(function(data) {
Promise.resolve(data);
}).catch(function(error) {
Promise.reject(error);
});
}
//
// Run the promises
//
findBookings()
.then(invoiceAndReceipt)
.then(sendResult)
.catch(err => {
console.log("getBookingList ERR: " + err);
res.json({ error: true, err })
});