如何在UI中显示异步数据 [英] how to show async data in UI

查看:136
本文介绍了如何在UI中显示异步数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个异步函数,该函数需要调用服务器中的程序,并且该程序会生成一个文件,该文件需要在UI中加载以进行显示.由于execFile是异步函数,我不确定如何在UI中显示结果,并且可能需要几秒钟的时间才能准备好?

I wrote an async function that needs to call a program in the server and that program generate a file which needs to load in UI for displaying. I am not sure how to show the result in my UI since execFile is async function and it may take few second results to be ready?

我是否需要某种无限循环来检查服务器中的结果是否准备就绪?

Do I need to have kind of infinity loop to check the result is ready in the server?

我正在使用nodejs-express把手.

I am using nodejs-express handlebars.

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);
});

我想做什么的图.

推荐答案

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

Avoid polling whenever possible. Sometimes you can't avoid it, but here you can. Just make use of the event handlers to find out what the state of the process is. You can register handlers for the following related events:

  • 断开连接
  • 错误
  • 关闭
  • 消息

用法示例是:

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

有关更多信息,请参见此详细说明在freeCodeCamp的网站上(不附属).

For more information, see this detailed explanation on freeCodeCamp's website (not affiliated).

这篇关于如何在UI中显示异步数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆