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 fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–3why, one step back1import {setTimeout} from 'node:timers/promises';2import {DiscardedError} from '../return/final-error.js';3Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 4why, one step back4// Validate `timeout` optionRename files (#977)ehmickybefore adoption4b2e316
lines 5–10why, one step back5export 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};10Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 11why, one step back11// Fails when the `timeout` option is exceededRename files (#977)ehmickybefore adoption4b2e316
lines 12–21why, one step back12export const throwOnTimeout = (subprocess, timeout, context, controller) => timeout === 0 || timeout === undefined13 ? []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};