Skip to content
Myr

timeout.js

This file

Path

lib/exit/timeout.js

20 lines · 779 bytes · JavaScript

Last changed by

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

line 1why, one step back
1import {setTimeout} from 'node:timers/promises';

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 2why, one step back
2import {DiscardedError} from '../return/cause.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 3–10why, one step back
3
4export const validateTimeout = ({timeout}) => {5 if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {6 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);7 }8};910// `timeout` option handling

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

line 11why, one step back
11export const throwOnTimeout = (subprocess, timeout, context, controller) => timeout === 0 || timeout === undefined

Split files (#837)ehmickybefore adoption672c2d7

line 12why, one step back
12 ? []

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

line 13why, one step back
13 : [killAfterTimeout(subprocess, timeout, context, controller)];

Split files (#837)ehmickybefore adoption672c2d7

line 14why, one step back
14

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

line 15why, one step back
15const killAfterTimeout = async (subprocess, timeout, context, {signal}) => {

Split files (#837)ehmickybefore adoption672c2d7

lines 16–17why, one step back
16 await setTimeout(timeout, undefined, {signal});
17 context.timedOut = true;

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

line 18why, one step back
18 subprocess.kill();

Split files (#837)ehmickybefore adoption672c2d7

lines 19–20why, one step back
19 throw new DiscardedError();
20};