0

How can I store a value that is returned in function? I am able to print the value but I am not able to store it inside the variable. Here is my code:

exports.realtimeValues = function (req,res ){
  var symbol = "shirt";
  const price = globalFuc.getOutStShare(symbol,res,function(val){
    console.log( val);
    return price ;
  });
  if(price ){
    console.log(price );
    return res.status(200).send('fetch data');   
  }else{
    return res.status(500).send('fetch data');    
  }

I get the value in the console but that value is not storing in the variable; Price is undefined.

Jonathan
  • 6,507
  • 5
  • 37
  • 47
hxnAbbas
  • 43
  • 5
  • 2
    `price` receives the return value of `globalFuc.getOutStShare`, not of the anonymous `function(val)`. If `getOutStShare` does asynchronous operations, you'll want to read up on callbacks, promises or async/await. – Thomas Mar 21 '19 at 11:17
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – georgeawg Mar 21 '19 at 15:08

0 Answers0