p-timeout hang and resolve guards: working versions (tested 2026-10-16)
Wrap hanging and fast promises with p-timeout to assert TimeoutError, custom messages, returned values and deterministic timer cleanup offline.
passed5 passed · 0 failed2026-10-16 17:50 UTC- Status
- passed
- Last passed
- 2026-10-16 17:50 UTC
- Tests
- 5 / 5
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
p-timeout@8.0.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38942732901
The code
Exactly the files that passed. Your agent can pull them with get_recipe("p-timeout-abort") (install), or see them on GitHub.
package.json
{
"name": "p-timeout-abort",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"p-timeout": "8.0.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/timeout.ts
import pTimeout, { TimeoutError } from "p-timeout";
export { TimeoutError };
export type TimeoutOptions = {
milliseconds: number;
message?: string | Error;
};
export function withTimeout<T>(
promise: Promise<T>,
options: TimeoutOptions,
): Promise<T> {
return pTimeout(promise, {
milliseconds: options.milliseconds,
message: options.message ?? "Operation timed out",
});
}
export function withTimeoutFallback<T>(
promise: Promise<T>,
milliseconds: number,
fallback: () => T | Promise<T>,
): Promise<T> {
return pTimeout(promise, { milliseconds, fallback });
}
test/timeout.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import pTimeout, { TimeoutError } from "p-timeout";
import { withTimeout, withTimeoutFallback } from "../src/timeout.ts";
test("rejects with TimeoutError when the wrapped promise hangs", async () => {
const hanging = new Promise<never>(() => {});
await assert.rejects(
pTimeout(hanging, { milliseconds: 20 }),
(error: unknown) => {
assert.ok(error instanceof TimeoutError);
assert.equal((error as Error).name, "TimeoutError");
return true;
},
);
});
test("supports a custom TimeoutError message", async () => {
const hanging = new Promise<never>(() => {});
await assert.rejects(
withTimeout(hanging, { milliseconds: 20, message: "too slow" }),
(error: unknown) => {
assert.ok(error instanceof TimeoutError);
assert.equal((error as Error).message, "too slow");
return true;
},
);
});
test("resolves with the value when the promise settles in time", async () => {
const result = await pTimeout(Promise.resolve(42), { milliseconds: 100 });
assert.equal(result, 42);
});
test("clears the timer once the promise resolves", async () => {
let scheduledMs: number | undefined;
let handle: ReturnType<typeof setTimeout> | undefined;
let clearedHandle: unknown;
const result = await pTimeout(Promise.resolve("ok"), {
milliseconds: 1000,
customTimers: {
setTimeout: (callback: () => void, milliseconds: number) => {
scheduledMs = milliseconds;
handle = setTimeout(callback, milliseconds);
return handle;
},
clearTimeout: (h: unknown) => {
clearedHandle = h;
clearTimeout(h as ReturnType<typeof setTimeout>);
},
},
});
assert.equal(result, "ok");
assert.equal(scheduledMs, 1000);
assert.equal(clearedHandle, handle);
});
test("falls back to a value instead of throwing", async () => {
const hanging = new Promise<never>(() => {});
const result = await withTimeoutFallback(hanging, 20, () => "fallback");
assert.equal(result, "fallback");
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.