Commit d4e04783 authored by Arunoda Susiripala's avatar Arunoda Susiripala

Make sure to run the promise logic if only called .then()

parent fc34484e
......@@ -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)
)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment