{"id":22222023,"url":"https://github.com/fabiospampinato/tryloop","last_synced_at":"2025-07-27T16:32:21.414Z","repository":{"id":41274858,"uuid":"204809275","full_name":"fabiospampinato/tryloop","owner":"fabiospampinato","description":"Simple library for retrying operations, it supports multiple backoff strategies.","archived":false,"fork":false,"pushed_at":"2024-06-30T22:07:56.000Z","size":22,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-07T13:18:22.510Z","etag":null,"topics":["backoff","exponential","linear","raf","retry","try"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/fabiospampinato.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","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},"funding":{"github":"fabiospampinato","custom":"https://www.paypal.me/fabiospampinato"}},"created_at":"2019-08-27T23:43:41.000Z","updated_at":"2024-08-19T17:19:20.000Z","dependencies_parsed_at":"2022-09-05T13:51:35.299Z","dependency_job_id":null,"html_url":"https://github.com/fabiospampinato/tryloop","commit_stats":{"total_commits":13,"total_committers":2,"mean_commits":6.5,"dds":0.07692307692307687,"last_synced_commit":"baf0b2b93dd3715c57f0d1953344783736f4a34a"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Ftryloop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Ftryloop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Ftryloop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Ftryloop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabiospampinato","download_url":"https://codeload.github.com/fabiospampinato/tryloop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227817017,"owners_count":17824200,"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":["backoff","exponential","linear","raf","retry","try"],"created_at":"2024-12-02T23:16:34.918Z","updated_at":"2024-12-02T23:16:35.570Z","avatar_url":"https://github.com/fabiospampinato.png","language":"TypeScript","readme":"# TryLoop\n\nSimple library for retrying operations, it supports multiple backoff strategies.\n\n## Install\n\n```sh\nnpm install --save tryloop\n```\n\n## Usage\n\nYou'll have to provide the `options.fn` function, which will be the operation to retry.\n\n- If the function throws, TryLoop will throw.\n- If the function returns `undefined`, or a `Promise` which resolves to `undefined`, the operation will be retried.\n- If the function returns any other value, or a `Promise` which resolves to any other value, TryLoop will return a `Promise` to that value.\n- If the function can't be retried any longer, e.g. maybe the timeout run out or we hit the max retries limit, TryLoop will return `undefined`.\n\nThere are multiple backoff/retry strategies implemented, each with its own particular retry logic.\n\nIn general you're going to use the library like this:\n\n```ts\nimport tryloop from 'tryloop';\n\nconst instance = tryloop.__strategy_name__ ({\n  fn: () =\u003e {}, // Operation to retry\n  // Other options...\n});\n\nconst result = await instance.start ();\n\n// instance.stop (); // Stops retrying the operation\n// instance.cancel (); // Just an alias for \"stop\"\n```\n\n## Strategies\n\n### Linear\n\nThe linear strategy retries the operation after a fixed interval.\n\nThese are the accepted options:\n\n```ts\ntype Options = {\n  fn: () =\u003e any, // Operation to retry\n  timeout: number, // Return undefined after this timeout\n  tries: number, // Return undefined after this number of tries\n  interval: number // Fixed interval between retries\n};\n```\n\nYou can use it like so:\n\n```ts\nimport tryloop from 'tryloop';\n\nconst instance = tryloop.linear ({\n  fn: () =\u003e {}, // Operation to retry\n  timeout: 10000, // Time out after 10s\n  tries: 100, // Not more than 100 tries\n  interval: 100 // Wait 100ms between retries\n});\n\nconst value = await instance.start ();\n```\n\n### Idle\n\nThe idle strategy retries the operation by calling `requestIdleCallback`.\n\nThese are the accepted options:\n\n```ts\ntype Options = {\n  fn: () =\u003e any, // Operation to retry\n  timeout: number, // Return undefined after this timeout\n  tries: number // Return undefined after this number of tries\n};\n```\n\nYou can use it like so:\n\n```ts\nimport tryloop from 'tryloop';\n\nconst instance = tryloop.idle ({\n  fn: () =\u003e {}, // Operation to retry\n  timeout: 10000, // Time out after 10s\n  tries: 100 // Not more than 100 tries\n});\n\nconst value = await instance.start ();\n```\n\n### Exponential\n\nThe exponential strategy retries the operation with exponentially increasing intervals.\n\nThese are the accepted options:\n\n```ts\ntype Options = {\n  fn: () =\u003e any, // Operation to retry\n  timeout: number, // Return undefined after this timeout\n  tries: number, // Return undefined after this number of tries\n  factor: number, // Base factor which will be exponentiated after each try\n  minInterval: number, // Minimum interval between retries, also starting interval\n  maxInterval: number // Maximum interval between retries\n};\n```\n\nYou can use it like so:\n\n```ts\nimport tryloop from 'tryloop';\n\nconst instance = tryloop.exponential ({\n  fn: () =\u003e {}, // Operation to retry\n  timeout: 10000, // Time out after 10s\n  tries: 100, // Not more than 100 tries\n  factor: 2, // Base factor\n  minInterval: 1, // Start with 1ms\n  maxInterval: 1000 // No more than 1s between retries\n});\n\nconst value = await instance.start ();\n```\n\n### requestAnimationFrame\n\nThe idle strategy retries the operation by calling `requestAnimationFrame`.\n\nThese are the accepted options:\n\n```ts\ntype Options = {\n  fn: () =\u003e any, // Operation to retry\n  timeout: number, // Return undefined after this timeout\n  tries: number // Return undefined after this number of tries\n};\n```\n\nYou can use it like so:\n\n```ts\nimport tryloop from 'tryloop';\n\nconst instance = tryloop.raf ({\n  fn: () =\u003e {}, // Operation to retry\n  timeout: 10000, // Time out after 10s\n  tries: 100 // Not more than 100 tries\n});\n\nconst value = await instance.start ();\n```\n\n### Random\n\nThe random strategy retries the operation using intervals randomly calculated between the min and the max.\n\nThese are the accepted options:\n\n```ts\ntype Options = {\n  fn: () =\u003e any, // Operation to retry\n  timeout: number, // Return undefined after this timeout\n  tries: number, // Return undefined after this number of tries\n  minInterval: number, // Minimum interval between retries\n  maxInterval: number // Maximum interval between retries\n};\n```\n\nYou can use it like so:\n\n```ts\nimport tryloop from 'tryloop';\n\nconst instance = tryloop.random ({\n  fn: () =\u003e {}, // Operation to retry\n  timeout: 10000, // Time out after 10s\n  tries: 100, // Not more than 100 tries\n  minInterval: 1, // Randomly pick an interval \u003e= 1ms\n  maxInterval: 1000 // Randomly pick an interval \u003c= 1ms\n});\n\nconst value = await instance.start ();\n```\n\n## License\n\nMIT © Fabio Spampinato\n","funding_links":["https://github.com/sponsors/fabiospampinato","https://www.paypal.me/fabiospampinato"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiospampinato%2Ftryloop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabiospampinato%2Ftryloop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiospampinato%2Ftryloop/lists"}