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

Get the child from promisified childProcess.execFile #28244

Closed
sindresorhus opened this issue Jun 15, 2019 · 1 comment · Fixed by #28325
Closed

Get the child from promisified childProcess.execFile #28244

sindresorhus opened this issue Jun 15, 2019 · 1 comment · Fixed by #28325

Comments

@sindresorhus
Copy link

sindresorhus commented Jun 15, 2019

Is your feature request related to a problem? Please describe.

When using childProcess.execFile(), it returns the child, but there's no way to access the child when using promisify(childProcess.execFile)(). I want to use the promise version for async/await, but I also need to access the child, specifically the .pid.

Describe the solution you'd like

It would be useful if the child could be attached to the promise as a property:

const execFile = promisify(childProcess.execFile);

(async () => {
	const promise = execFile('foo');

	console.log(promise.child.pid);

	await promise;
})();

Describe alternatives you've considered

The child could also be returned in the promise result along with stdout and stderr, but there are cases where it would be useful to access the child synchronously.

I could keep using the callback-version, but I don't think that's a good solution. Async/await is the future. The promise version should at least be as powerful as the callback version.

This applies to childProcess.exec too.

@Hakerh400
Copy link
Contributor

Sounds like a reasonable use case to me. Adding a new symbol to promises returned from util.promisify would be semver-minor change I guess. If you need a solution right now, here is a simple polyfill:

const kResult = Symbol('kResult');

const promisify = func => (...args) => {
  let resolve, reject;
  return Object.assign(new Promise((res, rej) => {
    resolve = res;
    reject = rej;
  }), {
    [kResult]: func(...args, (err, result) => {
      const f = () => {
        if(!resolve) return setTimeout(f);
        if(err) reject(err);
        else resolve(result);
      }
      f();
    }),
  });
};

const promise = promisify(cp.exec)('node -v');
console.log(promise[kResult].pid);
promise.then(a => console.log(String(a)));

cjihrig added a commit to cjihrig/node that referenced this issue Jun 22, 2019
This commit updates the custom exec() and execFile()
promisification to attach the ChildProcess instance to the
returned Promise.

PR-URL: nodejs#28325
Fixes: nodejs#28244
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Wyatt Preul <wpreul@gmail.com>
targos pushed a commit that referenced this issue Jul 2, 2019
This commit updates the custom exec() and execFile()
promisification to attach the ChildProcess instance to the
returned Promise.

PR-URL: #28325
Fixes: #28244
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Wyatt Preul <wpreul@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants