vault-link/frontend/test-client/src/utils/with-timeout.ts
2026-01-18 22:13:33 +00:00

25 lines
No EOL
671 B
TypeScript

import { __debug_locks } from "sync-client";
export async function withTimeout<T>(
promise: Promise<T>,
timeoutMs: number,
operationName: string
): Promise<T> {
return Promise.race([
promise,
new Promise<T>((_, reject) =>
setTimeout(() => {
reject(
new TimeoutError(`${operationName} timed out after ${timeoutMs}ms ${__debug_locks.map(lock => lock.getDebugString()).join(", ")}`)
);
}, timeoutMs)
)
]);
}
export class TimeoutError extends Error {
constructor(message: string) {
super(message);
this.name = "TimeoutError";
}
}