{"id":24719716,"url":"https://github.com/lionralfs/precurring","last_synced_at":"2025-06-24T10:35:48.591Z","repository":{"id":34800439,"uuid":"143200282","full_name":"lionralfs/precurring","owner":"lionralfs","description":":repeat: A tiny 280 byte library for recurring Promises","archived":false,"fork":false,"pushed_at":"2024-08-22T22:24:41.000Z","size":1719,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-09-18T17:26:19.883Z","etag":null,"topics":["javascript","polling","promise","recurring"],"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/lionralfs.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}},"created_at":"2018-08-01T19:35:41.000Z","updated_at":"2024-06-16T10:03:03.000Z","dependencies_parsed_at":"2024-01-26T07:29:35.606Z","dependency_job_id":"488ec22e-66b0-4418-87a6-b01f70344fd0","html_url":"https://github.com/lionralfs/precurring","commit_stats":{"total_commits":83,"total_committers":5,"mean_commits":16.6,"dds":0.6144578313253012,"last_synced_commit":"acc3e75dc3c1d4188464f898078d24d384262e32"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lionralfs%2Fprecurring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lionralfs%2Fprecurring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lionralfs%2Fprecurring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lionralfs%2Fprecurring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lionralfs","download_url":"https://codeload.github.com/lionralfs/precurring/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235839890,"owners_count":19053337,"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":["javascript","polling","promise","recurring"],"created_at":"2025-01-27T11:19:27.511Z","updated_at":"2025-01-27T11:19:28.167Z","avatar_url":"https://github.com/lionralfs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `precurring` - Recurring Promises\n\n[![npm](https://img.shields.io/npm/v/precurring.svg)](https://www.npmjs.com/package/precurring)\n![build status](https://github.com/lionralfs/precurring/actions/workflows/main.yml/badge.svg)\n\n## Installation\n\n```sh\nnpm install --save precurring\n```\n\nAlternatively, the UMD build is available on unpkg:\n\n```html\n\u003cscript src=\"https://unpkg.com/precurring/dist/precurring.umd.js\"\u003e\u003c/script\u003e\n```\n\nYou can find the library on `window.precurring`.\n\n## Examples\n\n### Pinging a server\n\n```js\nimport precurring from 'precurring';\n\nconst ping = precurring({\n  fn: () =\u003e fetch('/ping'),\n  interval: 5000, // fetch every 5 sec\n  timeout: 20000, // wait 20 sec max (optional)\n  onSuccess: console.log,\n  onError: console.error,\n});\n\nping.start();\n```\n\n### Stopping after X errors\n\n```js\nimport precurring from 'precurring';\n\nlet counter = 0;\n\nconst instance = precurring({\n  fn: myFunction,\n  interval: 1000,\n  onSuccess: console.log,\n  onError: () =\u003e {\n    if (++counter === 3) {\n      instance.stop();\n    }\n  },\n});\n\ninstance.start();\n```\n\n## Docs\n\n### `precurring`\n\nTakes an Object with all the properties listed below and returns an Object with `start` and `stop` methods.\n\n- `start()` starts the interval of calling `fn` repeatedly.\n- `stop()` stops it accordingly.\n\n### `fn` (`(...args: any[]) =\u003e Promise\u003cany\u003e`)\n\nA function returning a Promise. This is a wrapper function over your own logic, which you want to repeatedly call. The value it resolves to is passed to `onSuccess`. If it rejects, the error is passed to `onError`.\n\n### `interval` (`number`)\n\nA number in milliseconds between calls of `fn`. To be more precise, it is the time between the previous Promise settling, and `fn` being called again. Due to the library using `setTimeout`, this specified number is not 100% reliable, see [accuracy](#accuracy).\n\n### `timeout` (`number` - optional)\n\nA number in milliseconds after which to cancel `fn`. Note that this does not actually stop the execution of `fn` but instead ignores its return value after it times out. The `timeout` value is also subject to the [accuracy](#accuracy) issues.\nSince this value is optional, the default behavior is to wait however long it takes for the Promise returned by `fn` to settle.\n\n### `onSuccess` (`(val: any) =\u003e any`)\n\nA callback function which is called with the value that `fn`'s Promise settles with.\n\n### `onError` (`(error: any) =\u003e any`)\n\nA callback function which is called with whatever `fn`'s Promise rejects with.\n\n## \u003ca id=\"accuracy\"\u003e\u003c/a\u003eAccuracy\n\nUnder the hood, `setTimeout` is used throughout this library. Because of how it is implemented, it does not guarantee the execution time of its callback function. Take this explainer from the [MDN docs](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals#settimeout):\n\n\u003e **Note:** The specified amount of time (or the delay) is **not** the _guaranteed time_ to execution, but rather the _minimum time_ to execution. The callbacks you pass to these functions cannot run until the stack on the main thread is empty.\n\n\u003e As a consequence, code like `setTimeout(fn, 0)` will execute as soon as the stack is empty, **not** immediately. [...]\n\nHowever, in most cases this is negligible. If you rely on a more accurate implementation, you should look for a different implementation/library.\n\n## References\n\n- David Walsh's [JavaScript fetch with Timeout](https://davidwalsh.name/fetch-timeout) which this library is based on\n\n## License\n\n[MIT](LICENSE) © Lion Ralfs\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flionralfs%2Fprecurring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flionralfs%2Fprecurring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flionralfs%2Fprecurring/lists"}