Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jan 9, 2025
1 parent 817a37d commit 8313466
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 28 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# `@nrfcloud/wait-for-it`

<https://jsr.io/@nrfcloud/wait-for-it>

## Install with NPM

```bash
npx jsr add --save-dev @nrfcloud/wait-for-it
```

## Usage

```typescript
import * as wait_for_it from "@nrfcloud/wait-for-it";
const tenant = await wait_for_it<Tenant>(() => repo.getByUUID(e.aggregateUUID));
```
67 changes: 66 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"backoff": "2.5.0"
},
"devDependencies": {
"@types/backoff": "2.5.5"
}
"@bifravst/prettier-config": "1.1.4",
"@types/backoff": "2.5.5",
"prettier": "3.4.2"
},
"prettier": "@bifravst/prettier-config"
}
50 changes: 25 additions & 25 deletions waitForIt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exponential } from "backoff";
import { exponential } from 'backoff'

/**
* @module
Expand All @@ -22,28 +22,28 @@ import { exponential } from "backoff";
* ```
*/
export const waitForIt = <A>(
fn: () => Promise<A>,
retryNumber: number = 9
fn: () => Promise<A>,
retryNumber: number = 9,
): Promise<A> =>
new Promise((resolve, reject) => {
const b = exponential({
randomisationFactor: 0,
initialDelay: 250,
maxDelay: 5000,
});
let lastErr: Error;
b.failAfter(retryNumber);
b.on("ready", async () => {
try {
const res = await fn();
return resolve(res);
} catch (e) {
lastErr = e as Error;
}
b.backoff();
});
b.on("fail", () => {
reject(lastErr);
});
b.backoff();
});
new Promise((resolve, reject) => {
const b = exponential({
randomisationFactor: 0,
initialDelay: 250,
maxDelay: 5000,
})
let lastErr: Error
b.failAfter(retryNumber)
b.on('ready', async () => {
try {
const res = await fn()
return resolve(res)
} catch (e) {
lastErr = e as Error
}
b.backoff()
})
b.on('fail', () => {
reject(lastErr)
})
b.backoff()
})

0 comments on commit 8313466

Please sign in to comment.