2

I am trying to process rowsArr (lets say 100 records) asynchronous but I want to load country list from database only one time to avoid database connections. I am trying below example code but it is opening database connections 100 times.

var countryList = null;

async.each(rowsArr, function(row, callback) {


    if( countryList == null )
    {
        console.log("open database here to get country list and then assign to below variable");

        countryList = countriesFromDatabase;

        callback( countryList );
    }
    else
    {
        callback( countryList );
    }


}, function(err){

      console.log("all rows are processed");

});

It is working fine with async.eachSeries but I want it in asynchronous manner. Any suggestion?

2 Answers 2

3
  1. Get Countries from database
  2. Process all rows

So following will work

function getCountries(callback) {
  console.log("open database here to get country list and then assign to below variable");
  callback(null,countriesFromDatabase);
}
getCountries(function(err, countriesFromDatabase) {
 // use countries in following async loop
  async.each(rowsArr, function(row, callback) {
    callback(null, row); //process row here
  }, function(err) {
    console.log("all rows are processed");
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

this approach is good but my above function is already inside another loop. So 100 records are to process in one loop and there are millions of records.
I'm assuming until you get countriesFromDatabase you can not process row, so still if I get countriesFromDatabase in first iteration, other rows will not have countriesFromDatabase(being asynchronous) So my guess u need to change function a bit
This is almost certainly the right answer to the question as posted. I'd either edit the question to reflect why this answer doesn't work, or accept it.
I will change my code logic as per giving suggestion. Thanks.
0

It does what it should actually, all hundred rows processing starts at the same time and they're passing the if block.

You might want to use locks. https://github.com/BorisKozo/node-async-locks

That way you can lock your countryList checking process to avoid multiple database connections.

Comments

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.