import { deferred } from "./deferred.ts";
export class DeadlineError extends Error {
    constructor() {
        super("Deadline");
        this.name = "DeadlineError";
    }
}
export function deadline(p, delay) {
    const d = deferred();
    const t = setTimeout(() => d.reject(new DeadlineError()), delay);
    return Promise.race([p, d]).finally(() => clearTimeout(t));
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVhZGxpbmUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJkZWFkbGluZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsUUFBUSxFQUFFLE1BQU0sZUFBZSxDQUFDO0FBRXpDLE1BQU0sT0FBTyxhQUFjLFNBQVEsS0FBSztJQUN0QztRQUNFLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQztRQUNsQixJQUFJLENBQUMsSUFBSSxHQUFHLGVBQWUsQ0FBQztJQUM5QixDQUFDO0NBQ0Y7QUFLRCxNQUFNLFVBQVUsUUFBUSxDQUFJLENBQWEsRUFBRSxLQUFhO0lBQ3RELE1BQU0sQ0FBQyxHQUFHLFFBQVEsRUFBUyxDQUFDO0lBQzVCLE1BQU0sQ0FBQyxHQUFHLFVBQVUsQ0FBQyxHQUFHLEVBQUUsQ0FBQyxDQUFDLENBQUMsTUFBTSxDQUFDLElBQUksYUFBYSxFQUFFLENBQUMsRUFBRSxLQUFLLENBQUMsQ0FBQztJQUNqRSxPQUFPLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxDQUFDLENBQUMsQ0FBQyxPQUFPLENBQUMsR0FBRyxFQUFFLENBQUMsWUFBWSxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUM7QUFDN0QsQ0FBQyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IGRlZmVycmVkIH0gZnJvbSBcIi4vZGVmZXJyZWQudHNcIjtcblxuZXhwb3J0IGNsYXNzIERlYWRsaW5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