2022-12-02 04:13:32 +08:00
|
|
|
export const patch_promise = window => {
|
|
|
|
|
|
|
|
|
|
if(window.Promise.Original != null) {
|
2022-12-07 05:06:15 +08:00
|
|
|
// already patched
|
|
|
|
|
return
|
2022-12-02 04:13:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PromiseWithStatus extends Promise {
|
|
|
|
|
constructor(fn) {
|
|
|
|
|
let status
|
|
|
|
|
let is_constructor_finished = false
|
|
|
|
|
super(
|
|
|
|
|
(resolve, reject) => {
|
|
|
|
|
fn(
|
|
|
|
|
(value) => {
|
|
|
|
|
status = {ok: true, value}
|
|
|
|
|
if(is_constructor_finished) {
|
|
|
|
|
this.status = status
|
|
|
|
|
}
|
|
|
|
|
resolve(value)
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
status = {ok: false, error}
|
|
|
|
|
if(is_constructor_finished) {
|
|
|
|
|
this.status = status
|
|
|
|
|
}
|
|
|
|
|
reject(error)
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
is_constructor_finished = true
|
|
|
|
|
this.status = status
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PromiseWithStatus.Original = Promise
|
|
|
|
|
|
|
|
|
|
window.Promise = PromiseWithStatus
|
|
|
|
|
}
|