diff --git a/lib/dynamic.js b/lib/dynamic.js index b375bb3c..0ac0eef0 100644 --- a/lib/dynamic.js +++ b/lib/dynamic.js @@ -64,13 +64,7 @@ export class SameLoopPromise { constructor (cb) { this.onResultCallbacks = [] this.onErrorCallbacks = [] - - if (cb) { - cb( - (result) => this.setResult(result), - (error) => this.setError(error) - ) - } + this.cb = cb } setResult (result) { @@ -88,6 +82,7 @@ export class SameLoopPromise { } then (onResult, onError) { + this.runIfNeeded() const promise = new SameLoopPromise() const handleError = () => { @@ -119,6 +114,7 @@ export class SameLoopPromise { } catch (onError) { + this.runIfNeeded() const promise = new SameLoopPromise() const handleError = () => { @@ -144,4 +140,15 @@ export class SameLoopPromise { return promise } + + runIfNeeded () { + if (!this.cb) return + if (this.ran) return + + this.ran = true + this.cb( + (result) => this.setResult(result), + (error) => this.setError(error) + ) + } }