Skip to content

Commit

Permalink
feat(async): retry doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Hfutsora committed Feb 8, 2023
1 parent 4b3a1bd commit 0b80cf4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
25 changes: 25 additions & 0 deletions docs/modules/Async.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,29 @@ const res = await Promise.all([

seq ➔ [200, 100, 150]
res ➔ [200, 100, 150]
```

### retry

```ts
RetryOption = {
/**
* The time to wait between retries, in milliseconds. The default is 0.
*/
interval?: number;
/**
* The number of attempts to make before giving up. The default is 3.
*/
times?: number;
}

<A>(fn: ((times: number) => Promise<A>) | (() => A), times?: number): Promise<A>;
<A>(fn: ((times: number) => Promise<A>) | (() => A), options: RetryOption): Promise<A>;
```
Attempts to get a successful response from task no more than times times before returning an error.
```ts
await retry(() => new Promise(r => r(1)), 3) ➔ 1
await retry(() => 2), { times: 3, interval: 300 }) ➔ 2
```
2 changes: 1 addition & 1 deletion docs/modules/Math.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ between(2, 1, 2, { from: true, to: true }) ➔ false
(min: number, max: number) => number
```
Returns a random integer from min to max (includes min and max).
Returns a random integer from min to max (includes min and excludes max).
```ts
randrange(0, 100) ➔ 50 // a random number between 0 and 100
Expand Down
21 changes: 12 additions & 9 deletions src/Async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export type RetryOption = {
*/
times?: number
}
type Fn<A> = ((times: number) => Promise<A>) | (() => A)
/**
* Attempts to get a successful response from task no more than times times before returning an error.
*
Expand All @@ -102,11 +103,12 @@ export type RetryOption = {
* assert.deepStrictEqual(res, 2)
* ```
*/
export async function retry<A>(fn: (() => PromiseLike<A>) | (() => A), times?: number): Promise<A>
export async function retry<A>(fn: (() => Promise<A>) | (() => A), options: RetryOption): Promise<A>
export async function retry<A>(fn: (() => Promise<A>) | (() => A), options: number | RetryOption = 1): Promise<A> {
let interval = 0
let times = 1
export async function retry<A>(fn: Fn<A>, times?: number): Promise<A>
export async function retry<A>(fn: Fn<A>, options: RetryOption): Promise<A>
export async function retry<A>(fn: Fn<A>, options: number | RetryOption = 1): Promise<A> {
let interval: number
let times: number
let retry = 1

if(typeof options === 'number') {
times = options
Expand All @@ -116,12 +118,13 @@ export async function retry<A>(fn: (() => Promise<A>) | (() => A), options: numb
}

return new Promise((resolve, reject) => {
const attempt = () => {
const attempt = async () => {
try {
resolve(fn())
const res = await fn(retry)
resolve(res)
} catch(error) {
if (times > 0) {
times--
if (retry < times) {
retry++
interval ? setTimeout(attempt, interval) : attempt()
} else {
reject(error)
Expand Down
8 changes: 6 additions & 2 deletions test/Async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ test('retry', async () => {
retry(() => 1).then(r => expect(r).toBe(1))
retry(() => 2, 3).then(r => expect(r).toBe(2))
retry(() => new Promise(r => r(3))).then(r => expect(r).toBe(3))
retry(() => new Promise(r => r(4)), { times: 3, interval: 200 }).then(r => expect(r).toBe(4))
retry(() => new Promise((_, r) => r(-1)), 3).catch(e => expect(e).toBe(-1))
retry(() => new Promise(r => r(4)), { interval: 200 }).then(r => expect(r).toBe(4))
retry((retry) => new Promise((_, e) => e(retry)), { times: 2 }).catch(e => expect(e).toBe(2))
retry((retry) => new Promise((then, error) => {
if (retry === 3) return then(retry)
error(-1)
}), { times: 3, interval: 200 }).catch(e => expect(e).toBe(3))
})

0 comments on commit 0b80cf4

Please sign in to comment.