Skip to content
Myr

timeout.js

This file

Path

lib/terminate/timeout.js

21 lines · 832 bytes · JavaScript

Last changed by

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–3why, one step back
1import {setTimeout} from 'node:timers/promises';
2import {DiscardedError} from '../return/final-error.js';3

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 4why, one step back
4// Validate `timeout` option

Rename files (#977)ehmickybefore adoption4b2e316

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

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 11why, one step back
11// Fails when the `timeout` option is exceeded

Rename files (#977)ehmickybefore adoption4b2e316

lines 12–21why, one step back
12export const throwOnTimeout = (subprocess, timeout, context, controller) => timeout === 0 || timeout === undefined
13 ? []14 : [killAfterTimeout(subprocess, timeout, context, controller)];1516const killAfterTimeout = async (subprocess, timeout, context, {signal}) => {17 await setTimeout(timeout, undefined, {signal});18 context.timedOut = true;19 subprocess.kill();20 throw new DiscardedError();21};