swc/bundler/tests/.cache/deno/5416c4abadbe08806856e8496e8fc5758d72a3bd.ts
강동윤 f8aa0509ce
fix(bundler): Fix stack overflow (#2080)
swc_bundler:
 - Prevent infinite recursions. (#1963)
2021-08-15 02:37:31 +00:00

22 lines
574 B
TypeScript

// Loaded from https://deno.land/std@0.101.0/async/deadline.ts
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<T>(p: Promise<T>, delay: number): Promise<T> {
const d = deferred<never>();
const t = setTimeout(() => d.reject(new DeadlineError()), delay);
p.finally(() => clearTimeout(t));
return Promise.race([p, d]);
}