【发布时间】:2020-10-01 06:22:00
【问题描述】:
我有一个异步函数,可以进行face_detection 命令行调用。否则它工作正常,但我不能让它等待响应。这是我的功能:
async uploadedFile(@UploadedFile() file) {
let isThereFace: boolean;
const foo: child.ChildProcess = child.exec(
`face_detection ${file.path}`,
(error: child.ExecException, stdout: string, stderr: string) => {
console.log(stdout.length);
if (stdout.length > 0) {
isThereFace = true;
} else {
isThereFace = false;
}
console.log(isThereFace);
return isThereFace;
},
);
console.log(file);
const response = {
filepath: file.path,
filename: file.filename,
isFaces: isThereFace,
};
console.log(response);
return response;
}
isThereFace 在我返回的响应中始终是undefined,因为响应在来自face_detection 的响应准备好之前发送到客户端。我怎样才能做到这一点?
【问题讨论】:
-
请考虑使用 execSync (nodejs.org/api/…) 来同步执行您的代码。
-
@MoxxiManagarm 在异步环境中执行同步操作是非常不鼓励的,因为它会阻塞整个节点进程...
标签: javascript node.js typescript async-await child-process