( action: () => Promise<T>, check: (value: T) => boolean, interval = 2500, )
| 421 | * sync check returns false. Resolves with the value that passed the check. |
| 422 | */ |
| 423 | export async function promiseWhile<T>( |
| 424 | action: () => Promise<T>, |
| 425 | check: (value: T) => boolean, |
| 426 | interval = 2500, |
| 427 | ): Promise<T> { |
| 428 | return new Promise<T>((resolve, promiseReject) => { |
| 429 | const run = async () => { |
| 430 | try { |
| 431 | const res = await action(); |
| 432 | if (check(res)) { |
| 433 | return resolve(res); |
| 434 | } |
| 435 | setTimeout(run, interval); |
| 436 | } catch (err: unknown) { |
| 437 | return promiseReject(err); |
| 438 | } |
| 439 | }; |
| 440 | run(); |
| 441 | }); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Return a promise that rejects after timeoutMs but otherwise behave the same. |
nothing calls this directly
no test coverage detected
searching dependent graphs…