mirror of
https://github.com/gristlabs/grist-core.git
synced 2024-10-27 20:44:07 +00:00
19 lines
466 B
TypeScript
19 lines
466 B
TypeScript
|
import * as bluebird from 'bluebird';
|
||
|
|
||
|
/**
|
||
|
* Wait some time for a check to pass. Allow a pause between checks.
|
||
|
*/
|
||
|
export async function waitForIt(check: () => Promise<void>, maxWaitMs: number,
|
||
|
stepWaitMs: number = 1000) {
|
||
|
const start = Date.now();
|
||
|
for (;;) {
|
||
|
try {
|
||
|
await check();
|
||
|
return;
|
||
|
} catch (e) {
|
||
|
if (Date.now() - start > maxWaitMs) { throw e; }
|
||
|
}
|
||
|
await bluebird.delay(stepWaitMs);
|
||
|
}
|
||
|
}
|