]> git.r.bdr.sh - rbdr/forum/blob - src/lib/utils/resolve_after.ts
95a477e1bdf2f2c7c9e6c7211c7891014535ef6c
[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
8 let counter = null;
9 let currentValue = 0;
10
11 if (timesUntilResolve <= 0) {
12 throw new Error('Resolve after requires a positive integer');
13 }
14
15 const promise: Promise<void> = new Promise((resolvePromise) => {
16
17 counter = () => {
18
19 if (++currentValue === timesUntilResolve) {
20 resolvePromise();
21 }
22 };
23 });
24
25 return {
26 counter,
27 promise
28 };
29 };