Files
smartpromise/ts/smartpromise.classes.cumulativedeferred.ts
T

32 lines
777 B
TypeScript
Raw Normal View History

import { defer } from './smartpromise.classes.deferred.js';
2023-04-04 20:22:57 +02:00
2023-04-04 20:35:54 +02:00
export class CumulativeDeferred {
private accumulatedPromises: Promise<unknown>[] = [];
private deferred = defer<void>();
2023-04-04 20:22:57 +02:00
public promise = this.deferred.promise;
constructor() {
setTimeout(async () => {
while (this.accumulatedPromises.length > 0) {
const poppedPromise = this.accumulatedPromises.shift();
await poppedPromise;
}
this.deferred.resolve();
}, 0);
}
public subDefer() {
const done = defer<void>();
this.addPromise(done.promise);
return done;
}
public addPromise(promiseArg: Promise<unknown>): void {
2023-04-04 20:22:57 +02:00
this.accumulatedPromises.push(promiseArg);
}
}
2023-04-04 20:35:54 +02:00
export const cumulativeDefer = () => {
return new CumulativeDeferred();
};