Skip to content

Commit

Permalink
feat(async): retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Hfutsora committed Feb 6, 2023
1 parent 96c804f commit 0d825c9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ Typescript library for functional programming.
- [x] Iterator
- [x] Pipe & Flow
- [x] Equal
- [x] Effect
- [x] Math
- [ ] Task (Promise-Like)
- [x] Async (Promise-Like)
- [ ] Docs
- [ ] Functors

Expand Down
56 changes: 56 additions & 0 deletions src/Async.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isNullable } from './function'
import { isEmpty } from './Iterator'
import { Either } from './Either'
import { Maybe } from './Maybe'

/** Asynchronous tasks queued for execution */
type AsyncTaskQueue = { f: () => Promise<any>, then: (v: any) => void, err: (e: any) => void }[]
Expand Down Expand Up @@ -75,3 +77,57 @@ export class AsyncQueue {
}
}
}


export type 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
}
/**
* Attempts to get a successful response from task no more than times times before returning an error.
*
* @example
*
* ```ts
* const res = await retry(() => new Promise(r => r(1)), 3)
* assert.deepStrictEqual(res, 1)
*
* const res = await retry(() => 2), { times: 3, interval: 300 })
* 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

if(typeof options === 'number') {
times = options
} else {
times = options.times ?? 1
interval = options.interval ?? 0
}

return new Promise((resolve, reject) => {
const attempt = () => {
try {
resolve(fn())
} catch(error) {
if (times > 0) {
times--
interval ? setTimeout(attempt, interval) : attempt()
} else {
reject(error)
}
}
}
attempt()
})
}
9 changes: 8 additions & 1 deletion test/Async.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { delay, microtask } from '../src/Delay'
import { AsyncQueue } from '../src/Async'
import { AsyncQueue, retry } from '../src/Async'

test('delay', () => {
let num = 0
Expand Down Expand Up @@ -58,3 +58,10 @@ test('async queue without limit', async () => {
expect(res).toEqual([200, 100, 150])
})

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))
})

0 comments on commit 0d825c9

Please sign in to comment.