Skip to content

Commit d4e0478

Browse files
committed
Make sure to run the promise logic if only called .then()
1 parent fc34484 commit d4e0478

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/dynamic.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ export class SameLoopPromise {
6464
constructor (cb) {
6565
this.onResultCallbacks = []
6666
this.onErrorCallbacks = []
67-
68-
if (cb) {
69-
cb(
70-
(result) => this.setResult(result),
71-
(error) => this.setError(error)
72-
)
73-
}
67+
this.cb = cb
7468
}
7569

7670
setResult (result) {
@@ -88,6 +82,7 @@ export class SameLoopPromise {
8882
}
8983

9084
then (onResult, onError) {
85+
this.runIfNeeded()
9186
const promise = new SameLoopPromise()
9287

9388
const handleError = () => {
@@ -119,6 +114,7 @@ export class SameLoopPromise {
119114
}
120115

121116
catch (onError) {
117+
this.runIfNeeded()
122118
const promise = new SameLoopPromise()
123119

124120
const handleError = () => {
@@ -144,4 +140,15 @@ export class SameLoopPromise {
144140

145141
return promise
146142
}
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+
}
147154
}

0 commit comments

Comments
 (0)