{"id":16013485,"url":"https://github.com/magicdawn/promise.retry","last_synced_at":"2025-03-17T20:31:17.632Z","repository":{"id":75520663,"uuid":"58716889","full_name":"magicdawn/promise.retry","owner":"magicdawn","description":"add (timeout and fail) retry for async functions","archived":false,"fork":false,"pushed_at":"2024-08-10T06:46:29.000Z","size":465,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-28T04:07:51.387Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magicdawn.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-13T08:16:35.000Z","updated_at":"2024-12-21T19:04:53.000Z","dependencies_parsed_at":"2024-01-24T10:52:13.942Z","dependency_job_id":"5213333d-55f8-4c4b-83d8-65a6e972c324","html_url":"https://github.com/magicdawn/promise.retry","commit_stats":{"total_commits":35,"total_committers":1,"mean_commits":35.0,"dds":0.0,"last_synced_commit":"cf4b701e064ee10e87f2f560f763e8450e360148"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicdawn%2Fpromise.retry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicdawn%2Fpromise.retry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicdawn%2Fpromise.retry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicdawn%2Fpromise.retry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magicdawn","download_url":"https://codeload.github.com/magicdawn/promise.retry/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243882231,"owners_count":20363108,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-08T14:41:34.547Z","updated_at":"2025-03-17T20:31:17.325Z","avatar_url":"https://github.com/magicdawn.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- AUTO_GENERATED_UNTOUCHED_FLAG --\u003e\n\n# promise.retry\n\n\u003e add (timeout and fail) retry for async functions\n\n[![Build Status](https://img.shields.io/github/actions/workflow/status/magicdawn/promise.retry/ci.yml?style=flat-square\u0026branch=main)](https://github.com/magicdawn/promise.retry/actions/workflows/ci.yml)\n[![Coverage Status](https://img.shields.io/codecov/c/github/magicdawn/promise.retry.svg?style=flat-square)](https://codecov.io/gh/magicdawn/promise.retry)\n[![npm version](https://img.shields.io/npm/v/promise.retry.svg?style=flat-square)](https://www.npmjs.com/package/promise.retry)\n[![npm downloads](https://img.shields.io/npm/dm/promise.retry.svg?style=flat-square)](https://www.npmjs.com/package/promise.retry)\n[![npm license](https://img.shields.io/npm/l/promise.retry.svg?style=flat-square)](http://magicdawn.mit-license.org)\n\n## Install\n\n```sh\n$ pnpm add promise.retry\n```\n\n## Note\n\nthis package require async/await environment.\n\n## Alternatives\n\n- [p-retry](https://www.npmjs.com/package/p-retry), `pretry(asyncAction, options)`\n- [radash.retry](https://radash-docs.vercel.app/docs/async/retry) `retry(options, asyncAction)`\n\n`asyncAction` means a function with no parameters, give `async funtion getUser(uid: string): Promise\u003cUser\u003e`, u need to wrap parameter in a asyncAction: `const user = await pretry(() =\u003e getUser('zhangsan'), options)`\n\nthis package take a different approach: `const tryGetUser = pretry(getUser, options)`, this is a async wrapper has same signature as `getUser`\n\n## API\n\n```js\nimport { pretry, pretryWithCleanUp, TimeoutError, RetryError } from 'promise.retry'\n```\n\n### `pretry`\n\n```js\nconst fnWithRetry = pretry(fn, options)\n```\n\n- `fn` the original async function\n- `options`\n  - `times` : `number` try how many times\n  - `timeout` : `number` the timeout for each attempt, in ms\n  - `delay`: `number` or `(i: number) =\u003e number`, retry delay, in ms.\n  - `onerror` : `(err: any, i: number) =\u003e any` add extra action on an attempt error\n\ni is always `0` based. (starts from `0`)\n\n### `AbortSignal`\n\nif `options.timeout` is provided, ptimeout will provide a extra runtime argument `signal?: AbortSignal`\nuse like below, see more at https://github.com/magicdawn/promise.timeout#singal\n\n```ts\nasync fn(num: number, signal?: AbortSignal) {\n  signal?.addEventListener('abort', () =\u003e {\n    // custom clean up\n  })\n}\n\nconst fn2 = pretry(fn, { timeout: 1000 }) // (num: number, signal?: AbortSignal) =\u003e Promise\u003cvoid\u003e\nawait fn2() // \u003c- no `signal` arg here, the `signal` in fn is provided by ptimeout at runtime, only when options.timeout specified\n\nconst fn3 = pretryWithCleanUp(fn, { timeout: 1000 }) // (num: number) =\u003e Promise\u003cvoid\u003e\nawait fn3() // \u003c- no `signal` arg here, the `signal` in fn is provided by ptimeout at runtime, only when options.timeout specified\n```\n\n### `pretryWithCleanUp`\n\n- only difference is it will trim last `AbortSignal?` arg, see `fn2` / `fn3` signature\n\n### TimeoutError\n\nre-export from `promise.timeout`, see https://github.com/magicdawn/promise.timeout#api\n\n### RetryError\n\nif all attempts failed, `p = fnWithRetry()`, `p` will be reject with a RetryError instance.\n\nprops\n\n- `times` : `number` same as `pretry` options\n- `timeout` : `number` same as `pretry` options\n- `message` : `string` the error message\n- `errors` : `[err1, err2, ...]` the errors\n\n## See Also\n\n- [promise.timeout](https://github.com/magicdawn/promise.timeout)\n- [promise.retry](https://github.com/magicdawn/promise.retry)\n- [promise.map](https://github.com/magicdawn/promise.map)\n- [promise.ify](https://github.com/magicdawn/promise.ify)\n- [promise.cb](https://github.com/magicdawn/promise.cb)\n- [promise.obj](https://github.com/magicdawn/promise.obj)\n- [promise.sleep](https://github.com/magicdawn/promise.sleep)\n\n## Changelog\n\n[CHANGELOG.md](CHANGELOG.md)\n\n## License\n\nthe MIT License http://magicdawn.mit-license.org\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicdawn%2Fpromise.retry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagicdawn%2Fpromise.retry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicdawn%2Fpromise.retry/lists"}