Document workaround for proper `all` interleaving (#640)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent f4955ad · Browse files · 1 file·+39 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+39 added−6 removed
- other:+39 added−6 removed
Provenance
- Commit
21d69c0- Tree
f30e45d- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
f4955ad
1 file changed+39 added−6 removed
modified:readme.md+39 added-6 removed
331 unmodified lines332 #### all333 334 Type: `ReadableStream | undefined`335 336-Stream combining/interleaving [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).336+Stream [combining/interleaving](#ensuring-all-output-is-interleaved) [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).337 338 This is `undefined` if either:339 - the [`all` option](#all-2) is `false` (the default value)340 - the [synchronous methods](#execasyncfile-arguments-options) are used77 unmodified lines418 #### all419 420 Type: `string | Uint8Array | undefined`421 422-The output of the process with `stdout` and `stderr` interleaved.422+The output of the process with `stdout` and `stderr` [interleaved](#ensuring-all-output-is-interleaved).423 424 This is `undefined` if either:425 - the [`all` option](#all-2) is `false` (the default value)426 - the [synchronous methods](#execasyncfile-arguments-options) are used212 unmodified lines639 640 Type: `boolean`\641 Default: `false`642 643-Add an `.all` property on the [promise](#all) and the [resolved value](#all-1). The property contains the output of the process with `stdout` and `stderr` interleaved.643+Add an `.all` property on the [promise](#all) and the [resolved value](#all-1). The property contains the output of the process with `stdout` and `stderr` [interleaved](#ensuring-all-output-is-interleaved).644 645 #### reject646 647 Type: `boolean`\191 unmodified lines839 ```840 841 ### Execute the current package's binary842 843+Execa can be combined with [`get-bin-path`](https://github.com/ehmicky/get-bin-path) to test the current package's binary. As opposed to hard-coding the path to the binary, this validates that the `package.json` `bin` field is correctly set up.844+845 ```js846 import {getBinPath} from 'get-bin-path';847 848 const binPath = await getBinPath();849 await execa(binPath);850 ```851 850-`execa` can be combined with [`get-bin-path`](https://github.com/ehmicky/get-bin-path) to test the current package's binary. As opposed to hard-coding the path to the binary, this validates that the `package.json` `bin` field is correctly set up.852+### Ensuring `all` output is interleaved853 854+The `all` [stream](#all) and [string/`Uint8Array`](#all-1) properties are guaranteed to interleave [`stdout`](#stdout) and [`stderr`](#stderr).855+856+However, for performance reasons, the child process might buffer and merge multiple simultaneous writes to `stdout` or `stderr`. This prevents proper interleaving.857+858+For example, this prints `1 3 2` instead of `1 2 3` because both `console.log()` are merged into a single write.859+860+```js861+import {execa} from 'execa';862+863+const {all} = await execa('node', ['example.js'], {all: true});864+console.log(all);865+```866+867+```js868+// example.js869+console.log('1'); // writes to stdout870+console.error('2'); // writes to stderr871+console.log('3'); // writes to stdout872+```873+874+This can be worked around by using `setTimeout()`.875+876+```js877+import {setTimeout} from 'timers/promises';878+879+console.log('1');880+console.error('2');881+await setTimeout(0);882+console.log('3');883+```884+885 ## Related886 854-- [gulp-execa](https://github.com/ehmicky/gulp-execa) - Gulp plugin for `execa`855-- [nvexeca](https://github.com/ehmicky/nvexeca) - Run `execa` using any Node.js version887+- [gulp-execa](https://github.com/ehmicky/gulp-execa) - Gulp plugin for Execa888+- [nvexeca](https://github.com/ehmicky/nvexeca) - Run Execa using any Node.js version889 - [sudo-prompt](https://github.com/jorangreef/sudo-prompt) - Run commands with elevated privileges.890 891 ## Maintainers892 2 unmodified lines