return promises for chaining in promise-tracker.ts
This commit is contained in:
parent
5f37eef244
commit
05414ce0b9
1 changed files with 6 additions and 3 deletions
|
|
@ -8,20 +8,23 @@ import { coreLogger } from '@/boot/coreLogger.js';
|
|||
const backgroundLogger = coreLogger.createSubLogger('background');
|
||||
const promiseRefs: Set<WeakRef<Promise<unknown>>> = new Set();
|
||||
|
||||
export function trackTask(task: () => Promise<unknown>): void {
|
||||
trackPromise(task());
|
||||
export function trackTask<T>(task: () => Promise<T>): Promise<T> {
|
||||
const promise = task();
|
||||
return trackPromise(promise);
|
||||
}
|
||||
|
||||
/**
|
||||
* This tracks promises that other modules decided not to wait for,
|
||||
* and makes sure they are all settled before fully closing down the server.
|
||||
* Returns the promise for chaining.
|
||||
*/
|
||||
export function trackPromise(promise: Promise<unknown>) {
|
||||
export function trackPromise<T>(promise: Promise<T>): Promise<T> {
|
||||
const ref = new WeakRef(promise);
|
||||
promiseRefs.add(ref);
|
||||
promise
|
||||
.catch(err => backgroundLogger.error('Unhandled error in tracked background task:', { err }))
|
||||
.finally(() => promiseRefs.delete(ref));
|
||||
return promise;
|
||||
}
|
||||
|
||||
export async function allSettled(): Promise<void> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue