Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:50:11+00:00 2026-06-17T17:50:11+00:00

I want to know how to handle dependencies in using the async library in

  • 0

I want to know how to handle dependencies in using the async library in Node.js, look at the following example:

db.open(function(error, client) {
  client.collection(('my-collection', function(error, collection) {
    collection.insert({ "email": "test@gmail.com" }, function(error, docs) {
      // Do stuff.
    });
  });
});

Using the async library:

async.parallel([
    function(callback) {
       db.open(function(error, client) {
         callback(error, client);
       });
    },
    function(callback) {
       // How do I access the "client" variable at this point?
    }
],
function(results){
   // Do stuff.
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:50:13+00:00Added an answer on June 17, 2026 at 5:50 pm

    You are using async parallell, which runs all functions together and they can finish in any order.

    you can use the async waterfall function which passes varibles from one callback to the next function eg.

        async.waterfall([
        function(callback){
            callback(null, 'one', 'two');
        },
        function(arg1, arg2, callback){
            callback(null, 'three');
        },
        function(arg1, callback){
            // arg1 now equals 'three'
            callback(null, 'done');
        }
    ], function (err, result) {
       // result now equals 'done'    
    });
    

    or you can use the auto function, which allowes you to specify which other functions must finish first eg.

    async.auto({
        get_data: function(callback){
            // async code to get some data
        },
        make_folder: function(callback){
            // async code to create a directory to store a file in
            // this is run at the same time as getting the data
        },
        write_file: ['get_data', 'make_folder', function(callback){
            // once there is some data and the directory exists,
            // write the data to a file in the directory
            callback(null, filename);
        }],
        email_link: ['write_file', function(callback, results){
            // once the file is written let's email a link to it...
            // results.write_file contains the filename returned by write_file.
        }]
    });
    

    so what you could do in your case is this:

    async.auto({
        dbReady: function(callback) {
           db.open(function(error, client) {
             callback(error, client);
           });
        },
        connected: ['dbReady', function(callback, results){
           // How do I access the "client" variable at this point?
           console.log(results.dbReady);
        }
    },
    function(results){
       // Do stuff.
    });
    

    Have a look at this to see all the available functions and their uses

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using Sitecore ECM to handle the newsletter subscribtion, what i want to know
I want to know how can I handle errors using jquery $post from Struts2
I want to know how objective C runtime handle arguments when I call a
I have a s:List component. I want to handle a click event to know
want to know why String behaves like value type while using ==. String s1
I want to know, Is there any way to handle or control the shockwave
I want to know what is the maximum value the number-format? For example int32
I just want to know which one is the best way to handle the
i want to know how to handle mobile screen orientation means in android mobile
I am a beginner in limeJS i want to know how to handle keyboard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.