首页 > 解决方案 > 如何同时渲染页面执行函数调用

问题描述

使用带有把手的 NestJs,我有下面的代码

@Post('/foo')
@Render('foo')
async foobar(){
    exemple();
    return {x: x, y: y};
}

我的问题是:我需要打电话exemaple ()而不是等待,继续return

我试图并行调用 async/await 函数,await Promise.all([...])作品只有函数调用,我需要调用exemple和呈现页面(作为回报)

编辑——exemple()函数是一个shell命令

var shell = require('shelljs')    
shell.exec('some command here')

标签: node.jstypescripthandlebars.jsnestjs

解决方案


根据文档,exec()它不会异步执行,除非提供回调或async选项设置为true.

因此,要解决您的问题,请将其设置为异步:

shell.exec('some command here', {async: true})

推荐阅读