0

I am running a cron job with node with mongodb as the database. I am trying to close db connection and exit the process once the curr_1 each loop has executed completely.

However the exit() is called while function_2 is being executed. I understand this is due to the callback and is async in nature.

How do I make sure exit is called only once the curr_1.each is complete? Any solution without promises?

function function_1(obj){
    var curr_1 = coll_1.find({})
    curr_1.each(function(err, doc) {
        function_2(doc)
    }); 
    exit(obj)
}
function function_2(obj) {
    coll_2.findOne({}, function(err, document) {
        dosomeprocess(obj)
    })
}
function exit(obj) {
    // Close connection
    console.log('closing connection')
    obj.db.close();
    process.exit();
}
2
  • 3
    What's wrong with promises? :( Commented Apr 12, 2016 at 10:17
  • my skype gofaizmh. I been trying bluebird promises. not easy to follow and i think you will end with the same problem with promises as well. Disclaimer - not sure on my understanding on promises. can you post solution with promises for above example Commented Apr 12, 2016 at 10:34

3 Answers 3

1

It's a job for Node async....

For example:

async.each(
  curr_1, // the collection to iterate over
  function(doc, callback) { // the function, which is passed each
                            // document of the collection, and a
                            // callback to call when doc handling
                            // is complete (or an error occurs)
    function_2(doc);
  },
  function(err) { // callback called when all iteratee functions
                  // have finished, or an error occurs
    if (err) {
      // handle errors...
    }

    exit(obj); // called when all documents have been processed
  }
);
Sign up to request clarification or add additional context in comments.

Comments

1

Without using any library:

function function_1(obj, callback) {
     var curr_1 = coll_1.find({})
            curr_1.each(function(err, doc) {
            callback(err, doc);
    });

}

function function_2(err, obj) {

    coll_2.findOne({}, function(err, document) {
         dosomeprocess(obj)
         exit(err, obj);
    })
}

function exit(err, obj) {
   // Close connection
    console.log('closing connection')
    obj.db.close();
    process.exit();
}

function_1(obj, function_2);

Using async module

var async = require('async');

async.waterfall([
    function function_1(callback) {
    var curr_1 = coll_1.find({})
    curr_1.each(function(err, doc) {
    if (err) {
        callback(err, null)
    } else {
      allback(null, doc)
    }

   });
  },
  function function_2(obj, callback) {
    coll_2.findOne({}, function(err, document) {
    if (err) {
       callback(err, null);
    } else {
       dosomeprocess(obj)
       callback(null, obj);
    }

   })
 }

], function done() {
   obj.db.close();
   process.exit();

});

2 Comments

in your #1 option process.exit() is called during the first loop execution and thread dies even before executing of loop
use async module approch
-1

Simply give a condition in your loop using counter.

function function_1(obj){
var curr_1 = coll_1.find({})
var curr_1Length = curr_1.length;
var counter  = 0;
curr_1.each(function(err, doc) {
++counter;
//Check condition everytime for the last occurance of loop
if(counter == curr_1Length - 1){
exit(obj)
}
    function_2(doc)
}); 
}

Hope it helps :)

2 Comments

i didnt downvote. but dont prefer the counter option since counter == curr_1Length - 1 will always be true; and would exit the process whereas there would be some callbacks still executing
How can you say that counter == curr_1Length - 1 is always be true. Run the code and examine there is no callback executing when counter == curr_1Length - 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.