aboutsummaryrefslogtreecommitdiff
path: root/src/lib/utils/resolve_after.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/utils/resolve_after.ts')
-rw-r--r--src/lib/utils/resolve_after.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/lib/utils/resolve_after.ts b/src/lib/utils/resolve_after.ts
new file mode 100644
index 0000000..95a477e
--- /dev/null
+++ b/src/lib/utils/resolve_after.ts
@@ -0,0 +1,29 @@
+export type ResolveAfterPromise = {
+ counter: () => void,
+ promise: Promise<void>
+};
+
+export const resolveAfter = function (timesUntilResolve: number): ResolveAfterPromise {
+
+ let counter = null;
+ let currentValue = 0;
+
+ if (timesUntilResolve <= 0) {
+ throw new Error('Resolve after requires a positive integer');
+ }
+
+ const promise: Promise<void> = new Promise((resolvePromise) => {
+
+ counter = () => {
+
+ if (++currentValue === timesUntilResolve) {
+ resolvePromise();
+ }
+ };
+ });
+
+ return {
+ counter,
+ promise
+ };
+};