Skip to content

Commit

Permalink
Make sure to run the promise logic if only called .then()
Browse files Browse the repository at this point in the history
  • Loading branch information
arunoda committed Apr 28, 2017
1 parent fc34484 commit d4e0478
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -88,6 +82,7 @@ export class SameLoopPromise {
}

then (onResult, onError) {
this.runIfNeeded()
const promise = new SameLoopPromise()

const handleError = () => {
Expand Down Expand Up @@ -119,6 +114,7 @@ export class SameLoopPromise {
}

catch (onError) {
this.runIfNeeded()
const promise = new SameLoopPromise()

const handleError = () => {
Expand All @@ -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)
)
}
}

0 comments on commit d4e0478

Please sign in to comment.