-2

I am calling a function within another function (inner function), and I need the result of the inner function to the out function.

Example:

var value;
database.query1(param1, param2, function(err, outerResults) {
    if (outerResults == null) {
        database.query2(params1, params2, function(err, innerResults) {
            value = innerResults;
        });
        console.info(value); // I am not able to get the value of  this innerResults outside the function
    }
});
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
Sudheep
  • 1
  • 1

1 Answers1

0

You can use promises, the following is specific to your code but you can use primisefy

var value ;

Promise.resolve()
.then(
  x => 
    new Promise((resolve,reject)=>{
      database.query1(
        param1,param2
        ,function(err,outerResults) {
          if (outerResults === null) {
            database.query2(
              params1,params2
              ,function(err, innerResults) {
                if(err){
                  reject("Unable to resolve outer or inner query");
                  return;
                }
                resolve(innerResults);
            });
          }else{
            resolve(outerResults);
          }
      });
    })
)
.then(result => {
  console.log("got results:",results)
})
.then(null,reject =>{
  console.error(reject);
})
HMR
  • 37,593
  • 24
  • 91
  • 160