粘贴在下面的代码中的以下代码调用速度太快了:
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) 的函数调用完成之前完成。所以我没有得到我需要的答案。那有意义吗?