-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from piotr-oles/feature/improve-api
feat: improve api
- Loading branch information
Showing
7 changed files
with
508 additions
and
4,229 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,40 @@ | ||
import { Logger } from "./logger"; | ||
|
||
function wait(timeout = 250) { | ||
return new Promise((resolve) => setTimeout(resolve, timeout)); | ||
} | ||
|
||
interface RetryOptions { | ||
retries?: number; | ||
delay?: number; | ||
} | ||
|
||
/** | ||
* The IO effects sometimes fail due to different external issues - for example, network or filesystem. | ||
* To make these tests more reliable, we can wrap these effects in the `retry` function. | ||
*/ | ||
async function retry<T>( | ||
effect: () => Promise<T>, | ||
logger: Logger, | ||
options: RetryOptions = {} | ||
): Promise<T> { | ||
const retries = options.retries || 3; | ||
const delay = options.delay || 250; | ||
|
||
let lastError: unknown; | ||
|
||
for (let retry = 1; retry <= retries; ++retry) { | ||
try { | ||
return await effect(); | ||
} catch (error) { | ||
logger.log(error.toString()); | ||
logger.log(`Retry ${retry} of ${retries}.`); | ||
lastError = error; | ||
await wait(delay); | ||
} | ||
} | ||
|
||
throw lastError; | ||
} | ||
|
||
export { wait, retry, RetryOptions }; |
Oops, something went wrong.