【问题标题】:how to show async data in UI如何在 UI 中显示异步数据
【发布时间】:2019-05-10 16:45:00
【问题描述】:

我写了一个异步函数,它需要在服务器中调用一个程序,并且该程序生成一个文件,该文件需要加载到 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);
});

我想做什么的图表。

【问题讨论】:

    标签: node.js async.js execfile


    【解决方案1】:

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

    • 断开连接
    • 错误
    • 关闭
    • 留言

    一个使用例子是:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-24
      • 2020-12-27
      • 1970-01-01
      • 2023-03-16
      • 2021-12-14
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      相关资源
      最近更新 更多