首页 文章

如何在UI中显示异步数据

提问于
浏览
0

我编写了一个异步函数,需要调用服务器中的程序,该程序生成一个需要在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);
});

我想做什么的图表 .

1 回答

  • -1

    尽可能避免轮询 . 有时你无法避免它,但在这里你可以 . 只需使用事件处理程序即可了解进程的状态 . 您可以为以下相关事件注册处理程序:

    • 断开连接

    • 错误

    • 关闭

    • 消息

    使用的一个例子是:

    child.on('exit', function (code, signal) {
      console.log('child process exited with ' +
                  `code ${code} and signal ${signal}`);
    });
    

    有关更多信息,请参阅freeCodeCamp网站(非附属)上的this detailed explanation .

相关问题