3 ms·
??? Wait, so what's the solution?
by moralestapia 2y ago
???
Wait, so what's the solution?
- simonbw 2y agoMy understanding is that it's "don't call system.exit() until you have finished writing everything to system.stdout".
- gcommer 2y agoEven better: never use process.exit(). Set process.exitCode instead. https://nodejs.org/api/process.html#processexitcode https://nodejs.org/api/process.html#processexitcode
- o11c 2y agoOr just throw an error. See also `process.on('exit', ...)`; you can conditionally set process.exitCode in it (only if there was no other failure) if you want.
- 3np 2y agoUse Stream.finished. $ node -e "const { Stream } = require('stream'); s = process.stdout; s.write('@'.repeat(128 * 1024)); Stream.finished(s, () => { process.exit(0); })" | wc -c 131072 https://nodejs.org/api/stream.html#streamfinishedstream-options-callback https://nodejs.org/api/stream.html#streamfinishedstream-opti...
- ksr 2y agoI use fs.writeFileSync: $ node -e "fs.writeFileSync(1, Buffer.from('@'.repeat(128 * 1024))); process.exit(0);" | wc -c 131072
- 3np 2y agoIt doesn't work for all streams but for the stdout/stderr case where they can be treated like file descriptors, I prefer this simpler approach over fiddling with the Streams API. (If blocking the process on writing synchronously to stdout is indeed what you actually want...)