Monday, 8 May 2017

How to wait for the result in a single function for all callbacks in node.js?

May be question title not appropriate but I can explain what I want to do in my code

// One Callback Function

  connection_db.query(get_measure_query,function(err,user_data1){
        if(err){
          // throw err;
          let response_data = status_codes.db_error_0001;
          console.log(response_data);
          res.end('error');
        }else {

    }

    });

// 2nd Callback Function

  connection_db.query(get_measure_query,function(err,user_data2){
        if(err){
          // throw err;
          let response_data = status_codes.db_error_0001;
          console.log(response_data);
          res.end(enc);
        }else {
      res.end('error');
    }
    });

// 3rd Callback Function

 connection_db.query(get_measure_query,function(err,user_data3){
        if(err){
          // throw err;
          let response_data = status_codes.db_error_0001;
          console.log(response_data);
          res.end('error');
        }else {

    }
    });

Now I need all the data user_data1, user_data2 and user_data3. How can I get this in a single function

function allData(){
// This should wait for all the callbacks and then execute

}

I hope I am able to understands my question. Please let me know if som eone not understands it well In advance, thanks for the help guys !



via VIKAS KOHLI

No comments:

Post a Comment