-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Showing
16 changed files
with
386 additions
and
411 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @param maxAttempts the maximum number of attempts | ||
* @param interval interval in milliseconds to observe between attempts | ||
*/ | ||
export type EventuallyOptions = { | ||
maxAttempts?: number; | ||
interval?: number; | ||
}; | ||
|
||
const wait = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms)); | ||
const DEFAULT_INTERVAL = 1000; | ||
const DEFAULT_MAX_ATTEMPTS = 10; | ||
|
||
/** | ||
* Runs a function on an interval until the maximum number of attempts has | ||
* been reached. | ||
* | ||
* Default interval = 1000 milliseconds | ||
* Default maxAttempts = 10 | ||
* | ||
* @param fn function to run | ||
* @param options EventuallyOptions | ||
*/ | ||
const eventually = async <T>(call: () => Promise<T>, options?: EventuallyOptions): Promise<T> => { | ||
const opts = { | ||
interval: options?.interval ? options.interval : DEFAULT_INTERVAL, | ||
maxAttempts: options?.maxAttempts ? options.maxAttempts : DEFAULT_MAX_ATTEMPTS, | ||
}; | ||
|
||
while (opts.maxAttempts-- >= 0) { | ||
try { | ||
return await call(); | ||
} catch (err) { | ||
if (opts.maxAttempts <= 0) throw err; | ||
} | ||
await wait(opts.interval); | ||
} | ||
|
||
throw new Error('An unexpected error has occurred.'); | ||
}; | ||
|
||
export default eventually; |
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
3 changes: 0 additions & 3 deletions
3
...napshot/asset.96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34/index.d.ts
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
....snapshot/asset.96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34/index.js
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
....snapshot/asset.96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34/index.ts
This file was deleted.
Oops, something went wrong.
19 changes: 3 additions & 16 deletions
19
...integ/test/aws-ses-actions/test/integ.actions.js.snapshot/aws-cdk-ses-receipt.assets.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
...s-cdk-testing/framework-integ/test/aws-ses-actions/test/integ.actions.js.snapshot/cdk.out
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.