]> git.r.bdr.sh - rbdr/forum/blob - src/lib/utils/resolve_after.ts
Apply formatting
[rbdr/forum] / src / lib / utils / resolve_after.ts
1 export type ResolveAfterPromise = {
2 counter: () => void;
3 promise: Promise<void>;
4 };
5
6 export const resolveAfter = function (timesUntilResolve: number): ResolveAfterPromise {
7 let counter = null;
8 let currentValue = 0;
9
10 if (timesUntilResolve <= 0) {
11 throw new Error('Resolve after requires a positive integer');
12 }
13
14 const promise: Promise<void> = new Promise((resolvePromise) => {
15 counter = () => {
16 if (++currentValue === timesUntilResolve) {
17 resolvePromise();
18 }
19 };
20 });
21
22 return {
23 counter,
24 promise
25 };
26 };