import { deferred } from "./deferred.ts"; export class DeadlineError extends Error { constructor() { super("Deadline"); this.name = "DeadlineError"; } } /** * Create a promise which will be rejected with DeadlineError when a given delay is exceeded. */ export function deadline(p: Promise, delay: number): Promise { const d = deferred(); const t = setTimeout(() => d.reject(new DeadlineError()), delay); p.finally(() => clearTimeout(t)); return Promise.race([p, d]); }