3

I try to execute long processes sequentially with node.js (docker exec commands).

I do:

const childProcess = require('child_process');

const execWithPromise = async command => {
    return new Promise(async resolve => {
        const process = childProcess.exec(command);

        process.on('exit', err => resolve(err));
        process.on('close', err => resolve(err));
    });
};

const run = async () => {
    await execWithPromise('/usr/local/bin/docker exec -i -t cucumber node long-running-script.js');
    await execWithPromise('/usr/local/bin/docker exec -i -t cucumber node long-running-script.js');
};

run();

But the promise is resolved immediately with a result of 1. In both cases. The command runs on the commandline just fine.

Why is it returning immediately?

Michael
  • 6,123
  • 9
  • 49
  • 77

2 Answers2

12

child_process.exec expects a callback as the second or third argument. It doesn't return a promise. You have a few choices depending on your use case and version of node.

Use a callback and return the resolve.

return new Promise(async resolve => {
     childProcess.exec(command, (err, stout, sterr) {
        resolve(err ? stout : sterr)
      }
  });

Use spawn instead (keeping most of your code)

const execWithPromise = async command => {
    return new Promise(async (resolve, reject) => {
        const process = childProcess.spawn(command);
        process.on('data', data => resolve(data));
        process.on('error', err => reject(err));
        process.on('close', err => reject(err));
    });
};

Use execSync with try catch

return new Promise(async (resolve, reject) => {
    try {
        resolve(childProcess.execSync(command));
    } catch(error) {
      reject(error) 
    }
});
matteocng
  • 47
  • 2
  • 2
  • 9
Peter Grainger
  • 3,799
  • 1
  • 17
  • 22
1

I know this is an old question but here is a useful tool I discovered with node a while back...So, say you have a node file app.ts, in typescript that is...

app.ts

import utils from 'util'; // The thing that is useful, it has a bunch of useful functions
import { exec } from 'child_process'; // The exec import

export function execute(command: string): Promise<any> {
    // Not too concerned about the return type here
    return utils.promisify(exec)(command);
}

const run = async () => {
    await execute('/usr/local/bin/docker exec -i -t cucumber node long-running-script.js');
    await execute('/usr/local/bin/docker exec -i -t cucumber node long-running-script.js');
};

run();

In js though it would probably something like this

app.js

const utils = require('util');
const exec = require('child_process').exec;

function execute(command) {
    return utils.promisify(exec)(command);
}

const run = async () => {
    await execute('/usr/local/bin/docker exec -i -t cucumber node long-running-script.js');
    await execute('/usr/local/bin/docker exec -i -t cucumber node long-running-script.js');
};

run();
bloo
  • 1,316
  • 2
  • 9
  • 15