]> git.r.bdr.sh - rbdr/forum/blame_incremental - src/lib/utils/resolve_after.ts
Don't remember what this WIP was about
[rbdr/forum] / src / lib / utils / resolve_after.ts
... / ...
CommitLineData
1export type ResolveAfterPromise = {
2 counter: () => void;
3 promise: Promise<void>;
4};
5
6export 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};