Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么承诺会被忽略。那么当你有承诺的时候。一切都在。然后_Javascript_Angularjs - Fatal编程技术网

Javascript 为什么承诺会被忽略。那么当你有承诺的时候。一切都在。然后

Javascript 为什么承诺会被忽略。那么当你有承诺的时候。一切都在。然后,javascript,angularjs,Javascript,Angularjs,这是我的承诺 findBookings() .then(Promise.all([findInvoiceSum, findReceipt])) .then(sendResult) .catch(err => { console.log("getBookingList ERR: " + err); res.json({error:true,err}) } ) 我会检查它是否运行findBookings(),当它得到解决时,

这是我的承诺

findBookings()  
    .then(Promise.all([findInvoiceSum, findReceipt]))
    .then(sendResult)
    .catch(err => {
        console.log("getBookingList ERR: " + err);
        res.json({error:true,err})
    }
)
我会检查它是否运行findBookings(),当它得到解决时,它将使用([findInvoiceSum,findReceipt])运行该链,当两者都完成时,它将运行sendResult

不幸的是,这就是发生的情况(从控制台日志)

为什么呢?为什么我的第一个承诺被忽视了

这是我的控制器:

// 
// 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})
    }
)
我会检查它以运行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));
        };
    });
  });
}
我会检查它以运行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));
        };
    });
  });
}

因为您没有包装
findInvoiceSum
findReceipt
在一个函数中,承诺本身被初始化, 您可以将承诺包装成函数并使用
Promise

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 })
    });

因为您没有包装
findInvoiceSum
findReceipt
在一个函数中,承诺本身被初始化, 您可以将承诺包装成函数并使用
Promise

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 })
    });

哦!!!所以对象需要包含承诺。全部。-findReceipt和findInvoiceSum存在问题-如果我将它们转换为函数,Promise.all链将无法工作。我会再次尝试你的建议,但上次我这么做了,承诺。当它们是函数时,所有的都没有执行链。
Promise。所有的
都不会为你调用函数。您需要将一系列承诺传递给它
Promise.all([funcCall(),funcCall()])//是的,您是对的-我将承诺转化为“函数返回承诺”,并且它正常工作了!!-非常感谢Yury:-)哦!!!所以对象需要包含承诺。全部。-findReceipt和findInvoiceSum存在问题-如果我将它们转换为函数,Promise.all链将无法工作。我会再次尝试你的建议,但上次我这么做了,承诺。当它们是函数时,所有的都没有执行链。
Promise。所有的
都不会为你调用函数。您需要将一系列承诺传递给它
Promise.all([funcCall(),funcCall()])//是的,您是对的-我将承诺转化为“函数返回承诺”,并且它正常工作了!!-非常感谢,尤里:-)