Skip to content

Commit

Permalink
feat: do not throw on illegal state
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Oct 20, 2022
1 parent a92d787 commit 3cd0983
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 51 deletions.
12 changes: 2 additions & 10 deletions src/DeferredPromise.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { IllegalStateError } from "./IllegalStateError";

export type DeferredPromiseState = "pending" | "resolved" | "rejected";

export type ResolveFunction<Data extends any, Result = void> = (
Expand Down Expand Up @@ -35,10 +33,7 @@ export class DeferredPromise<Data extends any = void> {
this.promise = new Promise<Data>((resolve, reject) => {
this.resolve = (data) => {
if (this.state !== "pending") {
throw new IllegalStateError(
"Cannot resolve a DeferredPromise: illegal state",
this.state
);
return;
}

this.state = "resolved";
Expand All @@ -48,10 +43,7 @@ export class DeferredPromise<Data extends any = void> {

this.reject = (reason) => {
if (this.state !== "pending") {
throw new IllegalStateError(
"Cannot reject a DeferredPromise: illegal state",
this.state
);
return;
}

this.state = "rejected";
Expand Down
8 changes: 0 additions & 8 deletions src/IllegalStateError.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./DeferredPromise";
export * from "./IllegalStateError";
40 changes: 8 additions & 32 deletions test/DeferredPromise.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import { DeferredPromise } from "../src";
import { IllegalStateError } from "../src/IllegalStateError";

function extractError<ErrorType extends Error>(fn: () => void): ErrorType {
try {
fn();
} catch (error) {
return error as ErrorType;
}
}

describe("Promise-compliance", () => {
it('can be listened to with ".then()"', (done) => {
Expand Down Expand Up @@ -108,44 +99,29 @@ describe("resolve()", () => {
expect(promise.result).toBe(123);
});

it("throws when resolving an already resolved promise", async () => {
it("does nothing when resolving an already resolved promise", async () => {
const promise = new DeferredPromise<number>();
expect(promise.state).toBe("pending");

promise.resolve(123);
expect(promise.state).toBe("resolved");
expect(promise.result).toBe(123);

const stateError = extractError<IllegalStateError>(() => {
promise.resolve(456);
});

// Throws an illegal state error.
expect(stateError).toBeInstanceOf(IllegalStateError);
expect(stateError.message).toBe(
"Cannot resolve a DeferredPromise: illegal state"
);
expect(stateError.state).toBe("resolved");

// The state remains resolved.
// Resolving an already resolved Promise does nothing.
promise.resolve(456);
expect(promise.state).toBe("resolved");
expect(promise.result).toBe(123);
});

it("throws when resolving an already rejected promise", () => {
const promise = new DeferredPromise<number>().catch(() => {});
expect(promise.state).toBe("pending");
promise.reject("first reason");

const stateError = extractError<IllegalStateError>(() => {
promise.reject("second reason");
});

expect(stateError).toBeInstanceOf(IllegalStateError);
expect(stateError.message).toBe(
"Cannot reject a DeferredPromise: illegal state"
);
expect(stateError.state).toBe("rejected");
expect(promise.state).toBe("rejected");
expect(promise.rejectionReason).toBe("first reason");

// The state remains rejected.
promise.reject("second reason");
expect(promise.state).toBe("rejected");
expect(promise.rejectionReason).toBe("first reason");
});
Expand Down

0 comments on commit 3cd0983

Please sign in to comment.