Skip to content
Myr

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 file

Take it

Split files (#837)ehmickybefore adoption672c2d7

lines 1–3why, one step back
1import {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 back
4// However, `error` might be emitted after `spawn`, e.g. with the `cancelSignal` option.

Split files (#837)ehmickybefore adoption672c2d7

lines 5–9why, one step back
5// 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 times

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 10why, one step back
10export const waitForExit = async subprocess => {

Split files (#837)ehmickybefore adoption672c2d7

line 11why, one step back
11 const [spawnPayload, exitPayload] = await Promise.allSettled([

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 12–13why, one step back
12 once(subprocess, 'spawn'),
13 once(subprocess, 'exit'),

Split files (#837)ehmickybefore adoption672c2d7

lines 14–20why, one step back
14 ]);
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 back
21 ? waitForSubprocessExit(subprocess)

Split files (#837)ehmickybefore adoption672c2d7

lines 22–24why, one step back
22 : exitPayload.value;
23};24

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 25why, one step back
25const waitForSubprocessExit = async subprocess => {

Split files (#837)ehmickybefore adoption672c2d7

line 26why, one step back
26 try {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 27why, one step back
27 return await once(subprocess, 'exit');

Split files (#837)ehmickybefore adoption672c2d7

line 28why, one step back
28 } catch {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 29why, one step back
29 return waitForSubprocessExit(subprocess);

Split files (#837)ehmickybefore adoption672c2d7

lines 30–31why, one step back
30 }
31};