Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJs函数等待回调返回值_Node.js - Fatal编程技术网

Node.js NodeJs函数等待回调返回值

Node.js NodeJs函数等待回调返回值,node.js,Node.js,我想在函数调用之外打印var a的值。如果我运行这个程序,它会打印未定义的内容 使函数异步,然后等待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(f

我想在函数调用之外打印var a的值。如果我运行这个程序,它会打印未定义的内容


使函数异步,然后等待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');

你的题目和问题之间有什么联系?您究竟在哪里打印?在提出问题之前,您应该先访问此处:如果您的问题解决了,请不要忘记接受答案如何将响应设置为浏览器?
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');