File tree 1 file changed +14
-7
lines changed
1 file changed +14
-7
lines changed Original file line number Diff line number Diff line change @@ -64,13 +64,7 @@ export class SameLoopPromise {
64
64
constructor ( cb ) {
65
65
this . onResultCallbacks = [ ]
66
66
this . onErrorCallbacks = [ ]
67
-
68
- if ( cb ) {
69
- cb (
70
- ( result ) => this . setResult ( result ) ,
71
- ( error ) => this . setError ( error )
72
- )
73
- }
67
+ this . cb = cb
74
68
}
75
69
76
70
setResult ( result ) {
@@ -88,6 +82,7 @@ export class SameLoopPromise {
88
82
}
89
83
90
84
then ( onResult , onError ) {
85
+ this . runIfNeeded ( )
91
86
const promise = new SameLoopPromise ( )
92
87
93
88
const handleError = ( ) => {
@@ -119,6 +114,7 @@ export class SameLoopPromise {
119
114
}
120
115
121
116
catch ( onError ) {
117
+ this . runIfNeeded ( )
122
118
const promise = new SameLoopPromise ( )
123
119
124
120
const handleError = ( ) => {
@@ -144,4 +140,15 @@ export class SameLoopPromise {
144
140
145
141
return promise
146
142
}
143
+
144
+ runIfNeeded ( ) {
145
+ if ( ! this . cb ) return
146
+ if ( this . ran ) return
147
+
148
+ this . ran = true
149
+ this . cb (
150
+ ( result ) => this . setResult ( result ) ,
151
+ ( error ) => this . setError ( error )
152
+ )
153
+ }
147
154
}
You can’t perform that action at this time.
0 commit comments