1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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

This commit is contained in:
Arunoda Susiripala 2017-04-27 17:12:20 -07:00
parent fc34484e5e
commit d4e047839c

View file

@ -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)
)
}
}