我编写了一个异步函数,该函数需要调用服务器中的程序,并且该程序会生成一个文件,该文件需要加载到 UI 中进行显示。我不确定如何在我的 UI 中显示结果,因为 execFile 是异步函数,可能需要几秒钟的时间才能准备好结果?
我是否需要有一种无限循环来检查服务器中的结果是否准备就绪?
我正在使用 nodejs-express 车把。
router.post('/',function(req, res, next) {
const child = execFile('program.exe', ['in.sql'], (error, stdout, stderr) => {
if (error)
{
console.log(error);
return error;
}
else
{
// TODO: how to send the result to UI?
console.log(stdout);
}
});
return res.sendStatus(200);
});