Node.js 我可以让我的NodeJS服务器等待函数调用吗?我的程序完成得太快了
下面粘贴的代码中的以下代码太快,无法调用:Node.js 我可以让我的NodeJS服务器等待函数调用吗?我的程序完成得太快了,node.js,async-await,Node.js,Async Await,下面粘贴的代码中的以下代码太快,无法调用: global.h2=jsonifiedver(l.number) 太慢了。我如何让它等待从jsonifiedver()函数调用中得到一个答案,这样我就可以得到一个正确的答案。我试着使用全局变量,这些都是有效的,但只有在每一次调用之后,而且我知道调用是如何工作的,只是程序结束得太快了,在第二次调用时,它就有了我想要的数据。我是nodejs的新手,非常感谢您的帮助。谢谢 const server = http.createServer((req,
global.h2=jsonifiedver(l.number)
太慢了。我如何让它等待从jsonifiedver()函数调用中得到一个答案,这样我就可以得到一个正确的答案。我试着使用全局变量,这些都是有效的,但只有在每一次调用之后,而且我知道调用是如何工作的,只是程序结束得太快了,在第二次调用时,它就有了我想要的数据。我是nodejs的新手,非常感谢您的帮助。谢谢
const server = http.createServer((req, res) => {
if (req.method == 'POST') {
var body = ''
req.on('data', function(data) {
body += data
global.l = JSON.parse(body)
l = JSON.parse(body)
global.h2=jsonifiedver(l.number) // This call is slow and doesnt
// finish in time
global.h3 = JSON.stringify(h2)
console.log('Partial body: ' + body, global.l, global.l.number)
console.log("POST")
res.end("Not The real end");
})
} else {
console.log("GET")
}
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json'); // 'text/plain');
console.log(global.l)
res.end(global.h3); //"End");
});
因此res.end(global.h3)在我对global.h2=jsonifiedver(l.number)的函数调用完成之前完成。所以我没有得到我需要的答案。这有意义吗?您遇到的问题是,当调用请求时,
req.on('data',function(){})
只是为数据事件添加了一个钩子,但您也在else
语句之后使用res.end()
返回响应。除非触发了req.on('end')
,这意味着请求数据已经结束,否则您不应该发送回响应。在数据
事件中,理想情况下只需将数据附加到正文
,然后在结束
事件处理程序上处理正文并返回响应。具体如下:
const server = http.createServer((req, res) => {
const methodType = req.method;
if (methodType === "GET") {
console.log("GET");
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
console.log(global.l);
res.end(global.h3);
} else if (methodType === 'POST') {
console.log("POST")
var body = ''
req.on('data', function(data) {
body += chunk;
});
req.on('end', function() {
global.l = JSON.parse(body);
l = JSON.parse(body);
global.h2=jsonifiedver(l.number);
global.h3 = JSON.stringify(h2);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
console.log(global.l);
res.end(global.h3);
});
}
});
如果您正在等待jsonifiedver()
调用完成,请确保将其定义为Promise
/Async
函数,然后可以使用wait
调用它,确保调用jsonifiedver()的包装器函数
也是定义为异步的。您遇到的问题是,当调用请求时,req.on('data',function(){})
只是为数据
事件添加了一个钩子,但是您也在else
语句之后使用res.end()
返回响应。除非触发了req.on('end')
,这意味着请求数据已经结束,否则您不应该发送回响应。在数据
事件中,理想情况下只需将数据附加到正文
,然后在结束
事件处理程序上处理正文并返回响应。具体如下:
const server = http.createServer((req, res) => {
const methodType = req.method;
if (methodType === "GET") {
console.log("GET");
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
console.log(global.l);
res.end(global.h3);
} else if (methodType === 'POST') {
console.log("POST")
var body = ''
req.on('data', function(data) {
body += chunk;
});
req.on('end', function() {
global.l = JSON.parse(body);
l = JSON.parse(body);
global.h2=jsonifiedver(l.number);
global.h3 = JSON.stringify(h2);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
console.log(global.l);
res.end(global.h3);
});
}
});
如果您正在等待jsonifiedver()
调用完成,请确保将其定义为Promise
/Async
函数,然后可以使用wait
调用它,确保调用jsonifiedver()的包装器函数
也定义为async
。jsonifiedver()
的具体功能是什么?这是我编写的一个程序,我只是从数学公式返回一个数字列表。这是一个巨大的清单。有没有一种方法可以调用一个函数并让它在nodejs中等待?jsonifiedver()
到底做什么?这是我写的一个程序,我只是从一个数学公式返回一个数字列表。这是一个巨大的清单。有没有一种方法可以调用函数并让它在nodejs中等待?哇,这种更改不需要使用异步。谢谢,我还在学习,但我也会研究异步函数,因为我想掌握它们,但是这个解决方案工作得非常好。我本来希望学习一些异步的,但是这很好,谢谢Siddhesh!哇,这种改变不需要使用异步。谢谢,我还在学习,但我也会研究异步函数,因为我想掌握它们,但是这个解决方案工作得非常好。我本来希望学习一些异步的,但是这很好,谢谢Siddhesh!