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