如何在执行shell脚本时使用javascript async/await

如何在执行shell脚本时使用javascript async/await,javascript,shell,async-await,Javascript,Shell,Async Await,因此,我有一台服务器正在侦听RabbitMQ请求: console.log(' [*] Waiting for messages in %s. To exit press CTRL+C', q); channel.consume(q, async function reply(msg) { const mongodbUserId = msg.content.toString(); console.log(' [x]

因此,我有一台服务器正在侦听RabbitMQ请求:

        console.log(' [*] Waiting for messages in %s. To exit press CTRL+C', q);
        channel.consume(q, async function reply(msg) {
            const mongodbUserId = msg.content.toString();
            console.log(' [x] Received %s', mongodbUserId);

            await exec('./new_user_run_athena.sh ' + mongodbUserId, function(
                error,
                stdout,
                stderr
            ) {
                console.log('Running Athena...');
                console.log('stdout: ' + stdout);
                console.log('stderr: ' + stderr);
                if (error !== null) {
                    console.log('exec error: ' + error);
                }
            });

            console.log(
                ' Finished running Athena for mongodbUserId=%s',
                mongodbUserId
            );

            channel.sendToQueue(
                msg.properties.replyTo,
                new Buffer(mongodbUserId),
                { correlationId: msg.properties.correlationId }
            );

            channel.ack(msg);
        });
问题是执行shell脚本
new\u user\u run\u athena.sh
的wait调用发生在我打印出
为mongodbUserId运行完athena之后。您可以在控制台日志中看到它的发生:

 [*] Waiting for messages in run_athena_for_new_user_queue. To exit press CTRL+C
 [x] Received 5aa96f36ed4f68154f3f2143
 Finished running Athena for mongodbUserId=5aa96f36ed4f68154f3f2143
Running Athena...
stdout: 
stderr:
甚至可以在执行shell脚本时使用异步等待语法吗

见:

wait操作符用于等待承诺。它只能在异步函数中使用

exec
函数不会返回承诺,因此您不能等待它

您可以编写一个函数并返回该承诺。

请参阅:

wait操作符用于等待承诺。它只能在异步函数中使用

exec
函数不会返回承诺,因此您不能等待它


您可以编写一个函数并返回该承诺。

因为
exec
看起来需要回调,所以您可以使用回调将其包装为承诺。然后,您可以等待该承诺,而不是直接等待
exec
调用。因此,以你为例,类似于:

// Await a new promise:
await new Promise((resolve, reject) => {
    exec('./new_user_run_athena.sh ' + mongodbUserId, function(
        error,
        stdout,
        stderr
    ) {
        console.log('Running Athena...');
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
            console.log('exec error: ' + error);
            // Reject if there is an error:
            return reject(error);
        }

        // Otherwise resolve the promise:
        resolve();
    });
});

由于
exec
看起来需要回调,因此可以使用回调将其包装成承诺。然后,您可以等待该承诺,而不是直接等待
exec
调用。因此,以你为例,类似于:

// Await a new promise:
await new Promise((resolve, reject) => {
    exec('./new_user_run_athena.sh ' + mongodbUserId, function(
        error,
        stdout,
        stderr
    ) {
        console.log('Running Athena...');
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
            console.log('exec error: ' + error);
            // Reject if there is an error:
            return reject(error);
        }

        // Otherwise resolve the promise:
        resolve();
    });
});

将exec包装在承诺中。下面是一个typescript示例:

import { exec as childProcessExec } from 'child_process'

const exec = async (command: string): Promise<string> => {
  return new Promise((resolve, reject) => {
    childProcessExec(command, (error, stdout, stderr) => {
      if (error !== null) reject(error)
      if (stderr !== '') reject(stderr)
      else resolve(stdout)
    })
  })
}

将exec包装在承诺中。下面是一个typescript示例:

import { exec as childProcessExec } from 'child_process'

const exec = async (command: string): Promise<string> => {
  return new Promise((resolve, reject) => {
    childProcessExec(command, (error, stdout, stderr) => {
      if (error !== null) reject(error)
      if (stderr !== '') reject(stderr)
      else resolve(stdout)
    })
  })
}

您的代码中有什么
exec
?因为它没有回报一个承诺。因此,在其上使用
wait
并没有任何作用。代码中的
exec
是什么?因为它没有回报一个承诺。因此,在其上使用
wait
不会产生任何效果。在这里很方便。在这里很方便。