- Notifications
You must be signed in to change notification settings - Fork3
seriously like the best async child process library
License
jcoreio/promisify-child-process
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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.
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')
You must now passmaxBuffer orencoding tospawn/fork if you want tocapturestdout orstderr.
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.
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']);}
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)
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'})}
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;}
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
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors7
Uh oh!
There was an error while loading.Please reload this page.