smartpromise/ts/smartpromise.classes.cumulativedeferred.ts

26 lines
645 B
TypeScript
Raw Permalink Normal View History

2023-04-04 18:22:57 +00:00
import { defer } from "./smartpromise.classes.deferred.js";
2023-04-04 18:35:54 +00:00
export class CumulativeDeferred {
2023-04-04 18:22:57 +00:00
private accumulatedPromises: Promise<any>[] = [];
private deferred = defer();
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 addPromise(promiseArg: Promise<any>) {
this.accumulatedPromises.push(promiseArg);
}
}
2023-04-04 18:35:54 +00:00
export const cumulativeDefer = () => {
return new CumulativeDeferred();
2023-04-04 18:22:57 +00:00
}