{"id":16997033,"url":"https://github.com/hta218/tiny-retry","last_synced_at":"2025-03-22T15:31:07.450Z","repository":{"id":42505064,"uuid":"194073126","full_name":"hta218/tiny-retry","owner":"hta218","description":"~0.5kb ✨ function to retry an async job until the job success or stop after a maximum number of tries","archived":false,"fork":false,"pushed_at":"2023-03-14T16:22:24.000Z","size":4743,"stargazers_count":31,"open_issues_count":1,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-18T13:19:10.810Z","etag":null,"topics":["async-jobs","javascript","pure-js","retry","tiny","utilities"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hta218.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2019-06-27T10:16:51.000Z","updated_at":"2024-04-13T12:28:19.000Z","dependencies_parsed_at":"2024-10-28T13:29:54.420Z","dependency_job_id":"8688e473-25ae-4e10-bae6-d179715f9926","html_url":"https://github.com/hta218/tiny-retry","commit_stats":{"total_commits":55,"total_committers":2,"mean_commits":27.5,"dds":0.07272727272727275,"last_synced_commit":"4992434ecf28eb7ee988dfcab2df5fc15911fb11"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hta218%2Ftiny-retry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hta218%2Ftiny-retry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hta218%2Ftiny-retry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hta218%2Ftiny-retry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hta218","download_url":"https://codeload.github.com/hta218/tiny-retry/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244978568,"owners_count":20541875,"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":["async-jobs","javascript","pure-js","retry","tiny","utilities"],"created_at":"2024-10-14T03:53:51.252Z","updated_at":"2025-03-22T15:31:06.971Z","avatar_url":"https://github.com/hta218.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tiny `retry` ✨\n\n[![npm](https://img.shields.io/npm/v/tiny-retry?color=green)](https://www.npmjs.com/package/tiny-retry) \n[![npm bundle size](https://img.shields.io/bundlephobia/minzip/tiny-retry)](https://www.npmjs.com/package/tiny-retry)\n[![npm](https://img.shields.io/npm/dt/tiny-retry)](https://www.npmjs.com/package/tiny-retry)\n![GitHub](https://img.shields.io/github/license/hta218/tiny-retry)\n\nA lightweight [module](https://www.npmjs.com/package/tiny-retry) (**~0.5kb**) to retry an async job until the job success or stop after a maximum number of tries\n\n![package size](https://i.imgur.com/kPMgkMm.png)\n\n## Usage\n\n- Install package\n\n\t```bash\n\tnpm install tiny-retry\n\t# or yarn add tiny-retry\n\t```\n\n- Use it\n\n\t```js\n\timport retry from \"tiny-retry\";\n\n\t// Async context\n\tconst result = await retry(asyncJob, options);\n\n\tif (result.success) {\n\t\t// Do something with job data\n\t\tconsole.log(result.data)\n\t} else {\n\t\t// Do other thing\n\t}\n\t```\n\n## Parameters\n\n```javascript\nconst result = await retry(asyncJob, { maxTries, delay, startAfter, process, errorHandler, check });\n```\n\n- `asyncJob` [Function]: async function that throw an `Error` if failed\n- `options` [Object]: Retry options\n  - `options.maxTries` [Number]: number of maximum time to try to run job\n  - `options.delay` [Number]: the number in miliseconds of time after between each tries\n  - `options.starAfter` [Number]: the number in miliseconds to start the 1st try\n  - `options.process` [Function]: A process function to run before each try with the `tries` count argument\n  - `options.errorHandler` [Function]: A function to handle error in each try with the `err` argument\n  - `options.check` [Function]: A function with the job reponse argument to verify whether the job response is expected or not (throw an `Error` if not)\n\n## Return value\n\n- `result` [Object]: Represent the value of the job done or not and include job's data (if job return)\n  - `result.success` [Boolean]: Whether the job success or not\n  - `result.tries` [Number]: Number of tries\n  - `[result.data]` [Number]: The return value of job if success\n\n```javascript\nconsole.log(result)\n\n// Expect: { success: true, data: \"Async job data\", tries }\n// If job failed: { success: false, tries }\n```\n\n## Example\n\n[![Edit codesandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/test-tiny-retry-pjbqs?file=/src/index.js:0-1198)\n\n```javascript\nimport retry from \"tiny-retry\";\n\nconst wait = (secs) =\u003e new Promise((resolve) =\u003e setTimeout(resolve, secs));\n\nlet count = 0;\nconst fakeJobThatDoneAfter6Tries = async () =\u003e {\n\tawait wait(2000);\n\tcount += 1;\n\tif (count \u003c 4) {\n\t\tthrow new Error('Job failed!');\n\t} else if (count \u003c 6) {\n\t\treturn false\n\t} else {\n\t\tconsole.log('Job done!');\n\t\treturn \"Job data\"\n\t}\n};\n\n(async () =\u003e {\n\tconsole.log(\"Start Job\");\n\tconsole.time(\"JOB_COST\");\n\tconst result = await retry(fakeJobThatDoneAfter6Tries, {\n\t\tprocess: (tries) =\u003e console.log(`[TRY]: ${tries} time(s)`),\n\t\terrorHandler: (err) =\u003e console.log(err.toString()),\n\t\tcheck: Boolean,\n\t\tmaxTries: 10,\n\t\tdelay: 1000,\n\t\tstartAfter: 0\n\t});\n\tconsole.log(\"Job result: \", result);\n\tconsole.log(\"Time expect: 0 + 2*6 + 1*(6-1) = 17s\");\n\tconsole.timeEnd(\"JOB_COST\"); // Expect 17s\n})();\n\n```\n\n## License\n\nBundled by me from the ideas' combination of me and my friend.\n\nCopyright (c) 2021 Leo Huynh @ [https://leohuynh.dev](https://leohuynh.dev) under [MIT LICENSE](/LICENSE.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhta218%2Ftiny-retry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhta218%2Ftiny-retry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhta218%2Ftiny-retry/lists"}