fix: Anify timeout logic running even when fetch resolved

Summary:

Test Plan:
This commit is contained in:
2024-05-26 13:22:03 -04:00
parent 00ff0e0295
commit 6e8fe4f7b0
3 changed files with 64 additions and 11 deletions

View File

@@ -0,0 +1,29 @@
import { describe, expect, it } from "bun:test";
import { PromiseTimedOutError, promiseTimeout } from "./promiseTimeout";
describe("promiseTimeout", () => {
it("promise resolves within timeout, returns value", () => {
expect(
promiseTimeout(
wait(1).then(() => 2),
2,
),
).resolves.toBe(2);
});
it("promise does not resolve within timeout, throws PromiseTimedOutError", () => {
expect(
promiseTimeout(
wait(2).then(() => 2),
1,
),
).rejects.toThrow(PromiseTimedOutError);
});
});
function wait(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}