File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -2,4 +2,5 @@ export * from "./types";
2
2
export * from "./semaphore" ;
3
3
export * from "./mutex" ;
4
4
export * from "./latch" ;
5
+ export * from "./deferred" ;
5
6
export { TimeoutError , isTimeout } from "./utilities/timeout" ;
You can’t perform that action at this time.
0 commit comments