]>
Commit | Line | Data |
---|---|---|
a7cf03c1 RBR |
1 | export type ResolveAfterPromise = { |
2 | counter: () => void, | |
3 | promise: Promise<void> | |
4 | }; | |
5 | ||
6 | export 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 | }; |