Skip to content

Commit 07b19c8

Browse files
committed
feat: deferred
1 parent 45d48e3 commit 07b19c8

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/deferred.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export class Deferred<T> implements PromiseLike<T> {
2+
private promise: Promise<T>;
3+
4+
private resolver: (value?: T | PromiseLike<T>) => void = () => {
5+
throw new Error("why wasn't i replaced");
6+
};
7+
private rejector: (reason?: any) => void = () => {
8+
throw new Error("why wasn't i replaced");
9+
};
10+
11+
constructor() {
12+
this.promise = new Promise<T>((resolve, reject) => {
13+
this.resolver = resolve;
14+
this.rejector = reject;
15+
});
16+
}
17+
18+
get then() {
19+
return this.promise.then.bind(this.promise);
20+
}
21+
22+
get catch() {
23+
return this.promise.catch.bind(this.promise);
24+
}
25+
26+
get finally() {
27+
return this.promise.finally.bind(this.promise);
28+
}
29+
30+
resolve(value: T) {
31+
this.resolver(value);
32+
}
33+
34+
reject(reason: any) {
35+
this.rejector(reason);
36+
}
37+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export * from "./types";
22
export * from "./semaphore";
33
export * from "./mutex";
44
export * from "./latch";
5+
export * from "./deferred";
56
export { TimeoutError, isTimeout } from "./utilities/timeout";

0 commit comments

Comments
 (0)