Skip to content
Myr

exit.js

This file

Path

lib/stream/exit.js

31 lines · 988 bytes · JavaScript

Last changed by

Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf

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–31why, 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 times10export const waitForExit = async spawned => {11 const [spawnPayload, exitPayload] = await Promise.allSettled([12 once(spawned, 'spawn'),13 once(spawned, 'exit'),14 ]);1516 if (spawnPayload.status === 'rejected') {17 return [];18 }1920 return exitPayload.status === 'rejected'21 ? waitForProcessExit(spawned)22 : exitPayload.value;23};2425const waitForProcessExit = async spawned => {26 try {27 return await once(spawned, 'exit');28 } catch {29 return waitForProcessExit(spawned);30 }31};