Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process: attach child in promisification #28325

Merged
merged 1 commit into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,11 @@ Unlike the exec(3) POSIX system call, `child_process.exec()` does not replace
the existing process and uses a shell to execute the command.

If this method is invoked as its [`util.promisify()`][]ed version, it returns
a `Promise` for an `Object` with `stdout` and `stderr` properties. In case of an
error (including any error resulting in an exit code other than 0), a rejected
promise is returned, with the same `error` object given in the callback, but
with an additional two properties `stdout` and `stderr`.
a `Promise` for an `Object` with `stdout` and `stderr` properties. The returned
`ChildProcess` instance is attached to the `Promise` as a `child` property. In
case of an error (including any error resulting in an exit code other than 0), a
rejected promise is returned, with the same `error` object given in the
callback, but with an additional two properties `stdout` and `stderr`.

```js
const util = require('util');
Expand Down Expand Up @@ -295,9 +296,10 @@ stderr output. If `encoding` is `'buffer'`, or an unrecognized character
encoding, `Buffer` objects will be passed to the callback instead.

If this method is invoked as its [`util.promisify()`][]ed version, it returns
a `Promise` for an `Object` with `stdout` and `stderr` properties. In case of an
error (including any error resulting in an exit code other than 0), a rejected
promise is returned, with the same `error` object given in the
a `Promise` for an `Object` with `stdout` and `stderr` properties. The returned
`ChildProcess` instance is attached to the `Promise` as a `child` property. In
case of an error (including any error resulting in an exit code other than 0), a
rejected promise is returned, with the same `error` object given in the
callback, but with an additional two properties `stdout` and `stderr`.

```js
Expand Down
27 changes: 17 additions & 10 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,24 @@ function exec(command, options, callback) {

const customPromiseExecFunction = (orig) => {
return (...args) => {
return new Promise((resolve, reject) => {
orig(...args, (err, stdout, stderr) => {
if (err !== null) {
err.stdout = stdout;
err.stderr = stderr;
reject(err);
} else {
resolve({ stdout, stderr });
}
});
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});

promise.child = orig(...args, (err, stdout, stderr) => {
if (err !== null) {
err.stdout = stdout;
err.stderr = stderr;
reject(err);
} else {
resolve({ stdout, stderr });
}
});

return promise;
};
};

Expand Down
20 changes: 16 additions & 4 deletions test/parallel/test-child-process-promisified.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,37 @@ const exec = promisify(child_process.exec);
const execFile = promisify(child_process.execFile);

{
exec(`${process.execPath} -p 42`).then(common.mustCall((obj) => {
const promise = exec(`${process.execPath} -p 42`);

assert(promise.child instanceof child_process.ChildProcess);
promise.then(common.mustCall((obj) => {
assert.deepStrictEqual(obj, { stdout: '42\n', stderr: '' });
}));
}

{
execFile(process.execPath, ['-p', '42']).then(common.mustCall((obj) => {
const promise = execFile(process.execPath, ['-p', '42']);

assert(promise.child instanceof child_process.ChildProcess);
promise.then(common.mustCall((obj) => {
assert.deepStrictEqual(obj, { stdout: '42\n', stderr: '' });
}));
}

{
exec('doesntexist').catch(common.mustCall((err) => {
const promise = exec('doesntexist');

assert(promise.child instanceof child_process.ChildProcess);
promise.catch(common.mustCall((err) => {
assert(err.message.includes('doesntexist'));
}));
}

{
execFile('doesntexist', ['-p', '42']).catch(common.mustCall((err) => {
const promise = execFile('doesntexist', ['-p', '42']);

assert(promise.child instanceof child_process.ChildProcess);
promise.catch(common.mustCall((err) => {
assert(err.message.includes('doesntexist'));
}));
}
Expand Down