Javascript 在x秒后终止childprocess.exec

Javascript 在x秒后终止childprocess.exec,javascript,node.js,ros,child-process,Javascript,Node.js,Ros,Child Process,我找不到像子进程等待时间或响应等待时间这样的东西,因为我需要这样的东西。 所以希望有人能在这里帮助我 如果您看到下面的代码,我将打印ros主题。 然而,这个话题是活跃的,但没有返回任何内容。 因此,如果子进程在1-2秒内没有收到任何内容,我如何终止/杀死它呢。 因为这正在吞噬我的记忆 result = await execute("rostopic echo -n1 --noarr --offset /odom"); function execute(command) {

我找不到像子进程等待时间或响应等待时间这样的东西,因为我需要这样的东西。 所以希望有人能在这里帮助我

如果您看到下面的代码,我将打印ros主题。 然而,这个话题是活跃的,但没有返回任何内容。 因此,如果子进程在1-2秒内没有收到任何内容,我如何终止/杀死它呢。 因为这正在吞噬我的记忆

result = await execute("rostopic echo -n1 --noarr --offset /odom");


function execute(command) {
    return new Promise(function(resolve, reject) {
        childProcess.exec(command, function(error, standardOutput, standardError) {
            if (error) {
                reject(error)
            }
            if (standardError) {
                reject(standardError)
            }
            resolve(standardOutput);
        });
    }).catch((e) => {logger.crit(FOCNO+"-"+e)});
}

谢谢你,伙计。它的作品!谢谢你,伙计。它的作品!
const x = 3 /*seconds*/ * 1000;

function execute(command) {
    return new Promise(function(resolve, reject) {
        command = command.split(' ');
        const ps = childProcess.spawn(command[0], command.slice(1));
        const killTimeout = setTimeout(() => ps.kill(), x);

        ps.stdout.on('data', data => {
            clearTimeout(killTimeout);
            resolve(data);
        });

        ps.stderr.on('data', data => reject(data));
    }).catch((e) => {logger.crit(FOCNO+"-"+e)});
}

result = await execute("rostopic echo -n1 --noarr --offset /odom");