-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aba651b
commit 5eccab8
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export class WaitForTimeoutError extends Error { | ||
public constructor() { | ||
super('Timeout error while waiting for condition'); | ||
this.name = 'WaitForTimeoutError'; | ||
} | ||
} | ||
|
||
export type WaitForOptions = { | ||
interval?: number; | ||
timeout?: number; | ||
}; | ||
|
||
export function waitFor( | ||
callback: () => boolean, | ||
{ interval = 10, timeout = 1000 }: WaitForOptions = {}, | ||
): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
const timeoutId = setTimeout(() => { | ||
reject(new WaitForTimeoutError()); | ||
}, timeout); | ||
|
||
const intervalId = setInterval(() => { | ||
if (callback()) { | ||
clearTimeout(timeoutId); | ||
clearInterval(intervalId); | ||
resolve(); | ||
} | ||
}, interval); | ||
}); | ||
} |