exit-async.js
This file
Path
lib/resolve/exit-async.js
48 lines · 1.7 KiB · JavaScript
Last changed by
Add more code comments (#984)ehmickybefore adoptionfdd8a9f
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–33why, one step back1import {once} from 'node:events';2import {DiscardedError} from '../return/final-error.js';34// If `error` is emitted before `spawn`, `exit` will never be emitted.5// However, `error` might be emitted after `spawn`, e.g. with the `cancelSignal` option.6// In that case, `exit` will still be emitted.7// Since the `exit` event contains the signal name, we want to make sure we are listening for it.8// This function also takes into account the following unlikely cases:9// - `exit` being emitted in the same microtask as `spawn`10// - `error` being emitted multiple times11export const waitForExit = async subprocess => {12 const [spawnPayload, exitPayload] = await Promise.allSettled([13 once(subprocess, 'spawn'),14 once(subprocess, 'exit'),15 ]);1617 if (spawnPayload.status === 'rejected') {18 return [];19 }2021 return exitPayload.status === 'rejected'22 ? waitForSubprocessExit(subprocess)23 : exitPayload.value;24};2526const waitForSubprocessExit = async subprocess => {27 try {28 return await once(subprocess, 'exit');29 } catch {30 return waitForSubprocessExit(subprocess);31 }32};33Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 34why, one step back34// Retrieve the final exit code and|or signal nameRename files (#977)ehmickybefore adoption4b2e316
lines 35–44why, one step back35export const waitForSuccessfulExit = async exitPromise => {36 const [exitCode, signal] = await exitPromise;3738 if (!isSubprocessErrorExit(exitCode, signal) && isFailedExit(exitCode, signal)) {39 throw new DiscardedError();40 }4142 return [exitCode, signal];43};44Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 45why, one step back45// When the subprocess fails due to an `error` eventRename files (#977)ehmickybefore adoption4b2e316
line 46why, one step back46const isSubprocessErrorExit = (exitCode, signal) => exitCode === undefined && signal === undefined;Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 47why, one step back47// When the subprocess fails due to a non-0 exit code or to a signal terminationRename files (#977)ehmickybefore adoption4b2e316
line 48why, one step back48export const isFailedExit = (exitCode, signal) => exitCode !== 0 || signal !== null;