Skip to content

Commit

Permalink
onSettled based on finally
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianMairinger committed Feb 3, 2024
1 parent 789909b commit 561ccb1
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions app/src/moreProms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function execQueue(defaultOptions: {
p.res(prom)


const promSettled = !options.continueOnError ? prom : "onSettled" in prom ? prom.onSettled : new Promise((res) => {prom.then(res, res)})
const promSettled = !options.continueOnError ? prom : "onSettled" in prom ? prom.onSettled : prom.finally()

localPromsToContinue.push(promSettled)

Expand Down Expand Up @@ -347,7 +347,7 @@ function mkExt(Prom: typeof Promise) {
let finallyInit = false
class SettledPromise<T = void> extends Prom<T> {
public settled: boolean = false
public onSettled: Promise<void>


public res: (t: T | PromiseLike<T>) => void
public rej: (err: any) => void
Expand All @@ -357,34 +357,26 @@ function mkExt(Prom: typeof Promise) {

let res: any
let rej: any

let resSettled: Function
const onSettled = new Prom<void>((res) => {
resSettled = res
}) as any

super(!finallyInit ? (r, rj) => {
res = r
rej = rj

if (executor) executor((a) => {
resSettled()
this.settled = true
if (this.res !== undefined) this.res(a)
else r(a)
}, (a) => {
resSettled()
this.settled = true
if (this.rej !== undefined) this.rej(a)
else rj(a)
})
} : executor)

this.res = res
this.rej = rej


this.onSettled = onSettled
}

get onSettled(): Promise<any> {
return this.finally()
}
}

Expand Down

0 comments on commit 561ccb1

Please sign in to comment.