Skip to content

Commit

Permalink
feat(async): support signal on deadline() (#3347)
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue authored May 2, 2023
1 parent c5dbe07 commit 264c714
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
28 changes: 22 additions & 6 deletions async/deadline.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { deferred } from "./deferred.ts";
import { delay } from "./delay.ts";

export interface DeadlineOptions {
/** Signal used to abort the deadline. */
signal?: AbortSignal;
}

export class DeadlineError extends Error {
constructor() {
super("Deadline");
this.name = "DeadlineError";
this.name = this.constructor.name;
}
}

Expand All @@ -25,8 +30,19 @@ export class DeadlineError extends Error {
* const result = await deadline(delayedPromise, 10);
* ```
*/
export function deadline<T>(p: Promise<T>, delay: number): Promise<T> {
const d = deferred<never>();
const t = setTimeout(() => d.reject(new DeadlineError()), delay);
return Promise.race([p, d]).finally(() => clearTimeout(t));
export function deadline<T>(
p: Promise<T>,
ms: number,
options: DeadlineOptions = {},
): Promise<T> {
const controller = new AbortController();
const { signal } = options;
if (signal?.aborted) {
return Promise.reject(new DeadlineError());
}
signal?.addEventListener("abort", () => controller.abort(signal.reason));
const d = delay(ms, { signal: controller.signal })
.catch(() => {}) // Do NOTHING on abort.
.then(() => Promise.reject(new DeadlineError()));
return Promise.race([p.finally(() => controller.abort()), d]);
}
70 changes: 60 additions & 10 deletions async/deadline_test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,84 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects } from "../testing/asserts.ts";
import { deferred } from "./deferred.ts";
import { delay } from "./delay.ts";
import { deadline, DeadlineError } from "./deadline.ts";

Deno.test("[async] deadline: return fulfilled promise", async () => {
const p = deferred();
const t = setTimeout(() => p.resolve("Hello"), 100);
const controller = new AbortController();
const { signal } = controller;
const p = delay(100, { signal })
.catch(() => {})
.then(() => "Hello");
const result = await deadline(p, 1000);
assertEquals(result, "Hello");
clearTimeout(t);
controller.abort();
});

Deno.test("[async] deadline: throws DeadlineError", async () => {
const p = deferred();
const t = setTimeout(() => p.resolve("Hello"), 1000);
const controller = new AbortController();
const { signal } = controller;
const p = delay(1000, { signal })
.catch(() => {})
.then(() => "Hello");
await assertRejects(async () => {
await deadline(p, 100);
}, DeadlineError);
clearTimeout(t);
controller.abort();
});

Deno.test("[async] deadline: thrown when promise is rejected", async () => {
const p = deferred();
const t = setTimeout(() => p.reject(new Error("booom")), 100);
const controller = new AbortController();
const { signal } = controller;
const p = delay(100, { signal })
.catch(() => {})
.then(() => Promise.reject(new Error("booom")));
await assertRejects(
async () => {
await deadline(p, 1000);
},
Error,
"booom",
);
clearTimeout(t);
controller.abort();
});

Deno.test("[async] deadline: with non-aborted signal", async () => {
const controller = new AbortController();
const { signal } = controller;
const p = delay(100, { signal })
.catch(() => {})
.then(() => "Hello");
const abort = new AbortController();
const result = await deadline(p, 1000, { signal: abort.signal });
assertEquals(result, "Hello");
controller.abort();
});

Deno.test("[async] deadline: with signal aborted after delay", async () => {
const controller = new AbortController();
const { signal } = controller;
const p = delay(100, { signal })
.catch(() => {})
.then(() => "Hello");
const abort = new AbortController();
const promise = deadline(p, 100, { signal: abort.signal });
abort.abort();
await assertRejects(async () => {
await promise;
}, DeadlineError);
controller.abort();
});

Deno.test("[async] deadline: with already aborted signal", async () => {
const controller = new AbortController();
const { signal } = controller;
const p = delay(100, { signal })
.catch(() => {})
.then(() => "Hello");
const abort = new AbortController();
abort.abort();
await assertRejects(async () => {
await deadline(p, 100, { signal: abort.signal });
}, DeadlineError);
controller.abort();
});

0 comments on commit 264c714

Please sign in to comment.