Mark Pazon
Mark Pazon

Reputation: 6205

Nested promise in a for loop not behaving as expected

I am having trouble extending a Promise inside a .then(). I am trying to perform DB updates in a for-loop and then close the database after all records are processed. However the application exits with process.exit() right away which means that process.exit() was executed even before all db updates were finished. I am pretty sure I am doing something wrong with the nested promise.

var myDB;

function doSomething() {
   return MongoClient.connect(DB_CONNECTION).then(function(db) {
        myDB = db;
        var collection = db.collection(COLLETION_NAME);
        for (var i = 0; i < 10; i++) {

            promise.then(function{
                collection.update({
                   symbol: items[i].symbol
                }, {
                   $set: { 
                      value: 123
                   }
                }, {
                   upsert: true
               });  
            });
        }
   })
}

var promise = doSomething();
promise.then(function(){
    console.log("DONE");
    myDB.close();
    process.exit();
});

Upvotes: 0

Views: 224

Answers (2)

Mark Pazon
Mark Pazon

Reputation: 6205

Updated code as per @RayonDabre 's suggestion

function doSomething() {
   return MongoClient.connect(DB_CONNECTION).then(function(db) {
        myDB = db;
        var collection = db.collection(COLLECTION_NAME);
        var promises = [];

        for (var i = 0; i < 10; i++) {
            var innerPromise = collection.update({
               symbol: items[i].symbol
            }, {
               $set: { 
                  value: 123
               }
            }, {
                upsert: true
            });  
            promises.push(innerPromise);
        }
        return Promise.all(promises);
   });
}


var promise = doSomething();
promise.then(function(){
    console.log("DONE");
    myDB.close();
    process.exit();
});

Upvotes: 0

Ben Moran
Ben Moran

Reputation: 11

It looks like you are getting a promise back from the MongoClient.connect method so why not use that to chain together. I've put a quick sample together below based on your code:

function doSomething(db) {
    return new Promise(function(resolve, reject){
        var collection = db.collection(COLLETION_NAME);
        for (var i = 0; i < 10; i++) {
            collection.update({
               symbol: items[i].symbol
            }, {
               $set: { 
                  value: 123
               }
            }, {
               upsert: true
           });
        }

        resolve(db);
    })
}

function connectToDB() {
    return MongoClient.connect(DB_CONNECTION);
}

function closeDB(db) {
    return new Promise(function(resolve, reject){
         db.close();
         resolve();
    });
}

connectToDB().then(function(db){
    return doSomething(db);
}).then(function(db){
    return closeDB(db);
}).then(function(){
    console.log("DONE");
    process.exit();
}).catch(function(error){
    console.log('Something went wrong: ' + error);
});

Upvotes: 1

Related Questions