Skip to content

Commit

Permalink
refactor: use enum for DeferredPromise status
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Dec 27, 2024
1 parent 3dc39b1 commit fd55a0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/

import { DeferredPromise } from "../../../src";
import { DeferredPromise, DeferredPromiseStatus } from "../../../src";

describe("DeferredPromise constructor", () => {
it("sets resolve and reject functions", () => {
Expand All @@ -23,13 +23,13 @@ describe("DeferredPromise constructor", () => {
describe("DeferredPromise.status", () => {
it("returns pending when not yet resolved", () => {
const deferred = new DeferredPromise();
expect(deferred.status).toBe("pending");
expect(deferred.status).toBe(DeferredPromiseStatus.Pending);
});

it("returns fulfilled when resolved", () => {
const deferred = new DeferredPromise();
deferred.resolve(null);
expect(deferred.status).toBe("fulfilled");
expect(deferred.status).toBe(DeferredPromiseStatus.Fulfilled);
});

it("returns rejected when rejected", async () => {
Expand All @@ -41,7 +41,7 @@ describe("DeferredPromise.status", () => {
} catch (err) {
errorCaught = true;
}
expect(deferred.status).toBe("rejected");
expect(deferred.status).toBe(DeferredPromiseStatus.Rejected);
expect(errorCaught).toBe(true);
});
});
12 changes: 8 additions & 4 deletions packages/zowe-explorer-api/src/utils/DeferredPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
*/

/* Status of the deferred promise */
export type DeferredPromiseStatus = "pending" | "fulfilled" | "rejected";
export enum DeferredPromiseStatus {
Pending = "pending",
Fulfilled = "fulfilled",
Rejected = "rejected",
}

/**
* @brief Externally control the resolution and rejection of a promise.
Expand All @@ -21,7 +25,7 @@ export type DeferredPromiseStatus = "pending" | "fulfilled" | "rejected";
* the promise's outcome depends on factors outside the immediate context.
*/
export class DeferredPromise<T> {
private mStatus: DeferredPromiseStatus = "pending";
private mStatus: DeferredPromiseStatus = DeferredPromiseStatus.Pending;

public promise: Promise<T>;
public resolve: (value: T | PromiseLike<T>) => void;
Expand All @@ -30,11 +34,11 @@ export class DeferredPromise<T> {
public constructor() {
this.promise = new Promise<T>((resolve, reject) => {
this.resolve = (value): void => {
this.mStatus = "fulfilled";
this.mStatus = DeferredPromiseStatus.Fulfilled;
resolve(value);
};
this.reject = (err): void => {
this.mStatus = "rejected";
this.mStatus = DeferredPromiseStatus.Rejected;
reject(err);
};
});
Expand Down

0 comments on commit fd55a0f

Please sign in to comment.