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