Skip to content

Commit

Permalink
rename to deadline from timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Jul 11, 2021
1 parent 41a8abb commit 93566d6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions async/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ const [branch1, branch2] = tee(gen());
})();
```

## timeout
## deadline

Create a promise which will be rejected with `TimeoutError` when a given delay
Create a promise which will be rejected with `DeadlineError` when a given delay
is exceeded.

```typescript
import { timeout } from "https://deno.land/std/async/mod.ts";
import { deadline } from "https://deno.land/std/async/mod.ts";

const delayedPromise = delay(1000);
// Above throws `TimeoutError` after 10 ms
const result = await timeout(delayedPromise, 10);
// Above throws `DeadlineError` after 10 ms
const result = await deadline(delayedPromise, 10);
```
18 changes: 18 additions & 0 deletions async/deadline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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]);
}
12 changes: 6 additions & 6 deletions async/timeout_test.ts → async/deadline_test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
import { deferred } from "./deferred.ts";
import { timeout, TimeoutError } from "./timeout.ts";
import { deadline, DeadlineError } from "./deadline.ts";

Deno.test("[async] timeout: return fulfilled promise", async () => {
Deno.test("[async] deadline: return fulfilled promise", async () => {
const p = deferred();
const t = setTimeout(() => p.resolve("Hello"), 100);
const result = await timeout(p, 1000);
const result = await deadline(p, 1000);
assertEquals(result, "Hello");
clearTimeout(t);
});

Deno.test("[async] timeout: throws TimeoutError", async () => {
Deno.test("[async] deadline: throws DeadlineError", async () => {
const p = deferred();
const t = setTimeout(() => p.resolve("Hello"), 1000);
await assertThrowsAsync(async () => {
await timeout(p, 100);
}, TimeoutError);
await deadline(p, 100);
}, DeadlineError);
clearTimeout(t);
});
2 changes: 1 addition & 1 deletion async/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export * from "./delay.ts";
export * from "./mux_async_iterator.ts";
export * from "./pool.ts";
export * from "./tee.ts";
export * from "./timeout.ts";
export * from "./deadline.ts";
18 changes: 0 additions & 18 deletions async/timeout.ts

This file was deleted.

0 comments on commit 93566d6

Please sign in to comment.