exit.js
This file
Path
lib/stream/exit.js
31 lines · 1018 bytes · JavaScript
Last changed by
Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
History of this fileTake it
Split files (#837)ehmickybefore adoption672c2d7
lines 1–3why, one step back1import {once} from 'node:events';23// If `error` is emitted before `spawn`, `exit` will never be emitted.Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf
line 4why, one step back4// However, `error` might be emitted after `spawn`, e.g. with the `cancelSignal` option.Split files (#837)ehmickybefore adoption672c2d7
lines 5–9why, one step back5// In that case, `exit` will still be emitted.6// Since the `exit` event contains the signal name, we want to make sure we are listening for it.7// This function also takes into account the following unlikely cases:8// - `exit` being emitted in the same microtask as `spawn`9// - `error` being emitted multiple timesRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 10why, one step back10export const waitForExit = async subprocess => {Split files (#837)ehmickybefore adoption672c2d7
line 11why, one step back11 const [spawnPayload, exitPayload] = await Promise.allSettled([Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 12–13why, one step back12 once(subprocess, 'spawn'),13 once(subprocess, 'exit'),Split files (#837)ehmickybefore adoption672c2d7
lines 14–20why, one step back14 ]);1516 if (spawnPayload.status === 'rejected') {17 return [];18 }1920 return exitPayload.status === 'rejected'Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 21why, one step back21 ? waitForSubprocessExit(subprocess)Split files (#837)ehmickybefore adoption672c2d7
lines 22–24why, one step back22 : exitPayload.value;23};24Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 25why, one step back25const waitForSubprocessExit = async subprocess => {Split files (#837)ehmickybefore adoption672c2d7
line 26why, one step back26 try {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 27why, one step back27 return await once(subprocess, 'exit');Split files (#837)ehmickybefore adoption672c2d7
line 28why, one step back28 } catch {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 29why, one step back29 return waitForSubprocessExit(subprocess);Split files (#837)ehmickybefore adoption672c2d7
lines 30–31why, one step back30 }31};