-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
util: add util.promisify() #12442
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
util: add util.promisify() #12442
Changes from 1 commit
059f296
99da8e8
3ea2301
e965ed1
e7c5145
fbcb4f5
fe5ca3f
faf6654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
exec(File)
Author: Benjamin Gruenbaum <inglor@gmail.com> Author: Anna Henningsen <anna@addaleax.net> PR-URL: #12442 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: William Kapke <william.kapke@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,6 +214,23 @@ child runs longer than `timeout` milliseconds. | |
| *Note: 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. | ||
|
|
||
| For example: | ||
|
|
||
| ```js | ||
| const util = require('util'); | ||
| const exec = util.promisify(require('child_process').exec); | ||
|
|
||
| async function lsExample() { | ||
| const {stdout, stderr} = await exec('ls'); | ||
| console.log('stdout:', stdout); | ||
| console.log('stderr:', stderr); | ||
| } | ||
| lsExample(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I might be pedantic here, but we are ignoring the returned promise.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thefourtheye Yes … got a better suggestion? Just leave this line out? ;)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmmm, to think of it, that sounds better to me. |
||
| ``` | ||
|
|
||
| ### child_process.execFile(file[, args][, options][, callback]) | ||
| <!-- YAML | ||
| added: v0.1.91 | ||
|
|
@@ -263,6 +280,19 @@ can be used to specify the character encoding used to decode the stdout and | |
| 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. | ||
|
|
||
| ```js | ||
| const util = require('util'); | ||
| const execFile = util.promisify(require('child_process').execFile); | ||
| async function getVersion() { | ||
| const {stdout} = await execFile('node', ['--version']); | ||
| console.log(stdout); | ||
| } | ||
| getVersion(); | ||
| ``` | ||
|
|
||
| ### child_process.fork(modulePath[, args][, options]) | ||
| <!-- YAML | ||
| added: v0.5.0 | ||
|
|
@@ -1269,4 +1299,5 @@ to `stdout` although there are only 4 characters. | |
| [`process.on('message')`]: process.html#process_event_message | ||
| [`process.send()`]: process.html#process_process_send_message_sendhandle_options_callback | ||
| [`stdio`]: #child_process_options_stdio | ||
| [`util.promisify()`]: util.html#util_util_promisify_original | ||
| [synchronous counterparts]: #child_process_synchronous_process_creation | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const child_process = require('child_process'); | ||
| const { promisify } = require('util'); | ||
|
|
||
| common.crashOnUnhandledRejection(); | ||
|
|
||
| const exec = promisify(child_process.exec); | ||
| const execFile = promisify(child_process.execFile); | ||
|
|
||
| { | ||
| exec(`${process.execPath} -p 42`).then(common.mustCall((obj) => { | ||
| assert.deepStrictEqual(obj, { stdout: '42\n', stderr: '' }); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| execFile(process.execPath, ['-p', '42']).then(common.mustCall((obj) => { | ||
| assert.deepStrictEqual(obj, { stdout: '42\n', stderr: '' }); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| exec('doesntexist').catch(common.mustCall((err) => { | ||
| assert(err.message.includes('doesntexist')); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| execFile('doesntexist', ['-p', '42']).catch(common.mustCall((err) => { | ||
| assert(err.message.includes('doesntexist')); | ||
| })); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that it is quite tedious to do so, but examples would be good for these
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addaleax@8580fb2