-4

I want to print the value of var a outside the function call. If I run this program, it prints undefined.

var http = require('http');

var foo = function(req, res){
    var a = xyz('5',function(aa){
        console.log(aa);
        return aa;
    });
    res.end(a);
    console.log(a);
}

function xyz(arg,callback){
    var aa = 55;
    return callback(aa);
}

http.createServer(foo).listen(8000);
console.log('start server');

1 Answers1

0

make your function async and then await xyz()

var http = require('http');

var foo = async function(req, res){
  var a = await xyz('5'); 
  res.end(a);
  console.log(a);
}

function xyz(arg){
  var aa = 55;
  return aa;
}

http.createServer(foo).listen(8000);
console.log('start server');
Basit
  • 862
  • 1
  • 12
  • 30