Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

seriously like the best async child process library

License

NotificationsYou must be signed in to change notification settings

jcoreio/promisify-child-process

Repository files navigation

CircleCICoverage Statussemantic-releaseCommitizen friendlynpm version

seriously like the best async child process library

(I'm joking, you may want to useexeca which has a lot more features. The minor advantages of this package are,it's a dual CJS/ESM package, and it provides wrappers for all the asyncchild_process functions.)

Based uponchild-process-async,but more thorough, because that package doesn't seem very actively maintained.

promisify-child-process provides adrop-in replacement for theoriginalchild_process functions, not just duplicate methods thatreturn aPromise. So when you callexec(...) we still return aChildProcess instance, just with.then(),.catch(), and.finally() added tomake it promise-friendly.

Install and Set-up

npm install --save promisify-child-process

If you are using a old version of Node without built-inPromises orObject.create, you will need to use polyfills (e.g.@babel/polyfill).

// OLD:const{ exec, spawn, fork, execFile}=require('child_process')// NEW:const{ exec, spawn, fork, execFile}=require('promisify-child-process')

Upgrading to v3

You must now passmaxBuffer orencoding tospawn/fork if you want tocapturestdout orstderr.

Resolution/Rejection

The child process promise will only resolve if the process exits with a code of 0.If it exits with any other code, is killed by a signal, or emits an'error' event,the promise will reject.

Capturing output

exec andexecFile capturestdout andstderr by default. Butspawn andfork don't capturestdout andstderr unless you pass anencoding ormaxBuffer option:

const{ spawn}=require('promisify-child-process');asyncfunction(){// captures outputconst{ stdout, stderr}=awaitspawn('ls',['-al'],{encoding:'utf8'});const{ stdout, stderr}=awaitspawn('ls',['-al'],{maxBuffer:200*1024});// BUG, DOESN'T CAPTURE OUTPUT:const{ stdout, stderr}=awaitspawn('ls',['-al']);}

Additional properties on rejection errors

If the child process promise rejects, the error may have the following additionalproperties:

  • code - the process' exit code (if it exited)
  • signal - the signal the process was killed with (if it was killed)
  • stdout - the capturedstdout (if output capturing was enabled)
  • stderr - the capturedstderr (if output capturing was enabled)

Wrapper

If for any reason you need to wrap aChildProcess you didn't create,you can use the exportedpromisifyChildProcess function:

const{ promisifyChildProcess}=require('promisify-child-process');asyncfunction(){const{ stdout, stderr}=awaitpromisifyChildProcess(some3rdPartyFunctionThatReturnsChildProcess(),{encoding:'utf8'})}

Examples

exec()

asyncfunction(){const{ stdout, stderr}=awaitexec('ls -al');// OR:constchild=exec('ls -al',{});// do whatever you want with `child` here - it's a ChildProcess instance just// with promise-friendly `.then()` & `.catch()` functions added to it!child.stdin.write(...);child.stdout.pipe(...);child.stderr.on('data',(data)=> ...);const{ stdout, stderr}=awaitchild;}

spawn()

asyncfunction(){const{ stdout, stderr, code}=awaitspawn('ls',['-al'],{encoding:'utf8'});// OR:constchild=spawn('ls',['-al'],{});// do whatever you want with `child` here - it's a ChildProcess instance just// with promise-friendly `.then()` & `.catch()` functions added to it!child.stdin.write(...);child.stdout.pipe(...);child.stderr.on('data',(data)=> ...);const{ stdout, stderr, code}=awaitchild;}

About

seriously like the best async child process library

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp