如何使代码以适当的顺序执行

时间:2015-11-08 00:04:35

标签: node.js loops for-loop asynchronous

我正在使用mongodb在node.js中编写一个Web应用程序。我有这段代码,但不幸的是它没有按顺序执行,我的意思是循环迭代比执行那些db函数更快,这几乎没什么问题。

hospitalsCollection.update({}, {$pull: {doctors: parseInt(id)}}, function(err, success) {
    treatmentsCollection.update({}, {$pull: {doctors: parseInt(id)}}, function(err, success) {
        hospitalsCollection.find({"_id": {$in: hospitalsIds}}).toArray(function(err, hospitalsList) {
            for(i = 0; i < hospitalsList.length; i++) {
                for(j = 0; j < hospitalsList[i].treatments.length; j++) {
                    var exists = false;
                    for(k = 0; k < hospitalsList[i].doctors.length; k++) {
                        doctorsCollection.find({"_id": parseInt(hospitalsList[i].doctors[k])}).toArray(function(err, doctorObj) {
                            for(l = 0 ; l < doctorObj.treatments.length; l++) {
                                if(doctorObj.treatments[l] == hospitalsList[i].treatments[j]) {
                                    exists = true;
                                    break;
                                }
                            }
                        });
                        if(exists)
                            break;
                    }
                    if(exists) {
                        break;
                    }
                    else {
                        hospitalsCollection.update({"_id": parseInt(hospitalsList[i]._id)}, {$pull: {treatments: parseInt(hospitalsList[i].treatments[j])}}, function(err, success) {
                            treatmentsCollection.update({"_id": parseInt(hospitalsList[i].teratments[j])}, {$pull: {hospitals: parseInt(hospitalsList[i]._id)}}, function(err, success) {
                                console.log(err);
                            });
                        });
                    }
                }
            }

            for(i = 0; i < treatments.length; i++) {
                doctorsCollection.aggregate([ {$project:{"treatments._id":1, "treatments.price":1}}, {$unwind:"$treatments"},{$match:{"treatments._id": parseInt(treatments[i])}}, {$sort:{"treatments.price":-1}}, {$limit:1} ], function(err, result) {
                    doctorsCollection.aggregate([ {$project:{"treatments._id":1, "treatments.price":1}}, {$unwind:"$treatments"},{$match:{"treatments._id": parseInt(treatments[i])}}, {$sort:{"treatments.price":1}}, {$limit:1} ], function(err, result2) {
                        var maxPrice = result[0].treatments.price;
                        var minPrice = result2[0].treatments.price;
                        treatmentsCollection.update({"_id": parseInt(treatments[i])}, {$set: {"maxPrice": parseInt(maxPrice)}}, function(err, success) {
                            treatmentsCollection.update({"_id": parseInt(treatments[i])}, {$set: {"minPrice": parseInt(minPrice)}}, function(err, success) {
                                console.log(err);
                            });
                        });
                    });
                });
            }
        });
    });
});

我真的不知道如何处理这件事。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

使用Promise(bluebird很棒)。并重构你的代码。

使用Promise,您可以执行以下操作。

dbUpdate1()
.then(function(resultFromUpdate1) {

    let multipleUpdates = []
    multipleUpdates.push(updateCollectionA())
    multipleUpdates.push(updateCollectionB())

    return Promise.all(multipleUpdates)

.spread(afterUpdatingBothCollection)


function updateCollectionA(argument) {
    // body...
}

function updateCollectionB(argument) {
    // body...
}

function afterUpdatingBothCollection(resultFrom1, resultFrom2) {
    // body...
}