Skip to content

Commit

Permalink
Breaking change to signature of waitFor()
Browse files Browse the repository at this point in the history
+ Take optional `interval` and `timeout` arguments in a single options object - allow for easier (common) customization of timeout without needing to think about interval.
+ Fixes #3825
  • Loading branch information
amcclain committed Nov 14, 2024
1 parent b527e75 commit 67e4e8f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
* `DashModel|GroupingChooserModel|FilterChooserModel|PanelModel|TabContainerModel.provider`
* `PersistenceProvider.clearRaw()`
* Renamed `ZoneGridModelPersistOptions.persistMappings`, adding the trailing `s` for consistency.
* Updated `JsonBlobService.listAsync` to inline `loadSpec` with all other args in a single object.
* Changed signature of `JsonBlobService.listAsync()` to inline `loadSpec` with all other args in a
single options object.
* Changed signature of `waitFor()` to take its optional `interval` and `timeout` arguments in a
single options object.

### 🎁 New Features

Expand Down
13 changes: 6 additions & 7 deletions promise/Promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,20 @@ export function wait<T>(interval: number = 0): Promise<T> {
}

/**
* Return a promise that will resolve after a condition has been met, polling at the specified
* interval.
*
* @param condition - function that should return true when condition is met
* Return a promise that will resolve after a condition has been met, or reject if timed out.
* @param condition - function returning true when expected condition is met.
* @param interval - milliseconds to wait between checks (default 50). Note that the actual time
* will be subject to the minimum delay for `setTimeout()` in the browser.
* @param timeout - milliseconds after which the Promise should be rejected (default 5000).
*/
export function waitFor(
condition: () => boolean,
interval: number = 50,
timeout: number = 5 * SECONDS
{interval = 50, timeout = 5 * SECONDS}: {interval?: number; timeout?: number} = {}
): Promise<void> {
const startTime = Date.now();
if (!isNumber(interval) || interval <= 0) throw new Error('Invalid interval');
if (!isNumber(timeout) || timeout <= 0) throw new Error('Invalid timeout');

const startTime = Date.now();
return new Promise((resolve, reject) => {
const resolveOnMet = () => {
if (condition()) {
Expand Down

0 comments on commit 67e4e8f

Please sign in to comment.