close

How to promisify Node’s child_process.exec and child_process.execFile functions with Bluebird?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to promisify Node’s child_process.exec and child_process.execFile functions with Bluebird? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to promisify Node’s child_process.exec and child_process.execFile functions with Bluebird ?

  1. How to promisify Node's child_process.exec and child_process.execFile functions with Bluebird ?

    promisify Node's child_process.exec and child_process.execFile functions with Bluebird This correctly and simply satisfies the need to access ChildProcess in the original question and makes other answers out of date providing that Node v12+ can be used.

  2. promisify Node's child_process.exec and child_process.execFile functions with Bluebird

    promisify Node's child_process.exec and child_process.execFile functions with Bluebird This correctly and simply satisfies the need to access ChildProcess in the original question and makes other answers out of date providing that Node v12+ can be used.

Method 1

Here’s another way:

function execPromise(command) {
    return new Promise(function(resolve, reject) {
        exec(command, (error, stdout, stderr) => {
            if (error) {
                reject(error);
                return;
            }

            resolve(stdout.trim());
        });
    });
}

Use the function:

execPromise(command).then(function(result) {
    console.log(result);
}).catch(function(e) {
    console.error(e.message);
});

Or with async/await:

try {
    var result = await execPromise(command);
} catch (e) {
    console.error(e.message);
}

Method 2

Since Node v12 the built-in util.promisify allows access to the ChildProcess object in the returned Promise for built-in functions where it would have been returned by the un-promisified call. From the docs:

The returned ChildProcess instance is attached to the Promise as a child property.

This correctly and simply satisfies the need to access ChildProcess in the original question and makes other answers out of date providing that Node v12+ can be used.

Adapting the example (and concise style) provided by the questioner, access to the ChildProcess can be achieved like:

const util = require('util');
const exec = util.promisify(require('child_process').exec);
const promise = exec('node ./commands/server.js');
const child = promise.child; 

child.stdout.on('data', function(data) {
    console.log('stdout: ' + data);
});
child.stderr.on('data', function(data) {
    console.log('stderr: ' + data);
});
child.on('close', function(code) {
    console.log('closing code: ' + code);
});

// i.e. can then await for promisified exec call to complete
const { stdout, stderr } = await promise;

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment