为什么这个承诺会尽早解决?

时间:2015-08-17 19:03:27

标签: node.js express mongoose

Product.find()的承诺在更新最高价格的循环完成之前解析。只有在数据操作完成后,我该如何解决这个问题呢?

Product.find({'prodType': req.params.type}).lean().exec(function (err, product) {

    if (err) {
        res.status(400);
    }

    for (var i = 0; i < product.length; i++) {

        (function(u) {

            Option.find({'prodId': product[i].productId}).lean().sort({price: -1}).limit(1).exec(function (err, highest) {

                product[u].price = highest;
                // not updated in returned object

            });

        })(i);

    }

}).then(function (product) {

    res.send(product);

});

1 个答案:

答案 0 :(得分:0)

.exec()回调中的所有代码移至.then,并向其返回新承诺,该承诺将在所有其他承诺完成时解析。

Product.find({'prodType': req.params.type}).lean().exec()
.then(function (product) {
    var promises = [];
    for (var i = 0; i < product.length; i++) {

        (function(u) {

            promises.push(Option.find({'prodId': product[i].productId}).lean().sort({price: -1}).limit(1).exec(function (err, highest) {

                product[u].price = highest;
                // not updated in returned object

            }));

        })(i);

    }
    return Promise.all(promises).then(function () {
        // we want to pass the original product array to the next .then
        return product; 
    });

}).then(function (product) {

    res.send(product);

}).catch(function (err) { // the power of promise error catching!
    // If any error occurs in any of the db requests or in the code, this will be called.
    res.status(400);
});

此外,由于您正在处理数组,因此.map使这更容易,并且无需使用内部IIFE。

Product.find({'prodType': req.params.type}).lean().exec()
.then(function (product) {
    var promises = product.map(function (p) {
        return Option.find({'prodId': p.productId}).lean().sort({price: -1}).limit(1).exec(function (err, highest) {

            p.price = highest;

        }));
    });
    return Promise.all(promises).then(function () {
        // we want to pass the original product array to the next .then
        return product; 
    });

}).then(function (product) {

    res.send(product);

}).catch(function (err) { // the power of promise error catching!
    // If any error occurs in any of the db requests or in the code, this will be called.
    res.status(400);
});