Created
February 17, 2026 12:11
-
-
Save kalradivyanshu/8dddaa84034710b186355075578f30e0 to your computer and use it in GitHub Desktop.
Deferred Promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type DeferredState = "pending" | "resolved" | "rejected"; | |
| export class Deferred<T> { | |
| public promise: Promise<T>; | |
| public resolve!: (value: T) => void; | |
| public reject!: (reason?: unknown) => void; | |
| private state: DeferredState = "pending"; | |
| private value?: T; | |
| constructor() { | |
| this.promise = new Promise<T>((resolve, reject) => { | |
| this.resolve = (value: T) => { | |
| this.state = "resolved"; | |
| this.value = value; | |
| resolve(value); | |
| }; | |
| this.reject = (reason?: unknown) => { | |
| this.state = "rejected"; | |
| reject(reason); | |
| }; | |
| }); | |
| } | |
| is_pending(): boolean { | |
| return this.state === "pending"; | |
| } | |
| is_resolved(): boolean { | |
| return this.state === "resolved"; | |
| } | |
| is_rejected(): boolean { | |
| return this.state === "rejected"; | |
| } | |
| static resolved<T>(data: T): Deferred<T> { | |
| const deferred = new Deferred<T>(); | |
| deferred.resolve(data); | |
| return deferred; | |
| } | |
| async(): Promise<T> { | |
| return this.promise; | |
| } | |
| sync(): T | undefined { | |
| return this.value; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment