Skip to content

Commit b9bf9fa

Browse files
committed
Update util.ts
1 parent 89ec8f9 commit b9bf9fa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/support/util.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,28 @@ export const leadingDebounce = <T extends (...args: any[]) => any>(
100100
lastInvocation = Date.now();
101101
} as T;
102102
};
103+
104+
export const waitForValue = <T>(
105+
value: () => T,
106+
interval = 100,
107+
maxAttempts = 20,
108+
): Promise<T> =>
109+
new Promise((resolve) => {
110+
let attempts = 0;
111+
112+
const checkForValue = () => {
113+
attempts++;
114+
115+
if (attempts > maxAttempts) {
116+
return resolve(value());
117+
}
118+
119+
if (value() === null) {
120+
return setTimeout(checkForValue, 100);
121+
}
122+
123+
resolve(value());
124+
};
125+
126+
checkForValue();
127+
});

0 commit comments

Comments
 (0)