From 561ccb142bf37b57d142002cc53fd688a0ce6e0f Mon Sep 17 00:00:00 2001 From: Maximilian Mairinger Date: Sat, 3 Feb 2024 21:49:56 +0100 Subject: [PATCH] onSettled based on finally --- app/src/moreProms.ts | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/app/src/moreProms.ts b/app/src/moreProms.ts index 1bd33db..2dba812 100644 --- a/app/src/moreProms.ts +++ b/app/src/moreProms.ts @@ -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) @@ -347,7 +347,7 @@ function mkExt(Prom: typeof Promise) { let finallyInit = false class SettledPromise extends Prom { public settled: boolean = false - public onSettled: Promise + public res: (t: T | PromiseLike) => void public rej: (err: any) => void @@ -357,24 +357,15 @@ function mkExt(Prom: typeof Promise) { let res: any let rej: any - - let resSettled: Function - const onSettled = new Prom((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) }) @@ -382,9 +373,10 @@ function mkExt(Prom: typeof Promise) { this.res = res this.rej = rej - - - this.onSettled = onSettled + } + + get onSettled(): Promise { + return this.finally() } }