{"id":19009111,"url":"https://github.com/gamtiq/povtor","last_synced_at":"2026-05-12T23:33:40.488Z","repository":{"id":57329062,"uuid":"146504771","full_name":"gamtiq/povtor","owner":"gamtiq","description":"Repeat function call depending on the previous call result and specified conditions","archived":false,"fork":false,"pushed_at":"2020-04-30T16:27:39.000Z","size":369,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T22:06:17.920Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/gamtiq.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}},"created_at":"2018-08-28T20:44:25.000Z","updated_at":"2020-03-15T17:51:13.000Z","dependencies_parsed_at":"2022-09-01T11:41:10.869Z","dependency_job_id":null,"html_url":"https://github.com/gamtiq/povtor","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gamtiq%2Fpovtor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gamtiq%2Fpovtor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gamtiq%2Fpovtor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gamtiq%2Fpovtor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gamtiq","download_url":"https://codeload.github.com/gamtiq/povtor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240035944,"owners_count":19737613,"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-11-08T19:06:37.526Z","updated_at":"2026-04-23T20:30:17.067Z","avatar_url":"https://github.com/gamtiq.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# povtor \u003ca name=\"start\"\u003e\u003c/a\u003e\n\n[![NPM version](https://badge.fury.io/js/povtor.png)](http://badge.fury.io/js/povtor)\n\nRepeat function call depending on the previous call result and specified conditions.\n\n```js\nretry({\n    action: axios,\n    actionParams: [{url: '/api/some/endpoint'}],\n    retryAttempts: [2000, 5000],\n    retryTest: (response) =\u003e response.data.error,\n    retryOnError: true\n})\n.promise.then((response) =\u003e console.log(response.data));\n```\n\n### Features\n\n* Repeats calling of any function whether it returns a promise or a usual value (`action` setting).\n* Use returned promise to get function's last result or last error when retry process is finished.\n* Specify context (`actionContext` setting) and parameters (`actionParams` setting) for function call.\n* Retry function call when it throws or returns rejected promise (use `retryOnError` setting) and/or\n  when it returns normal value or fulfilled promise (use `retryTest` setting).\n* Use a function as value for `retryOnError`/`retryTest` setting to specify condition when to retry action call.\n* Specify quantity and timeouts of retry attempts (use `retryAttempts`, `retryQty` and/or `retryTimeout` settings).\n* Limit overall time of process of retry by `timeLimit` setting.\n* Control the process of calls repeating and stop it when it is necessary.\n* Access values and errors that are produced by function during the process.\n* Does not have dependencies except for [ES 2015 Promise API](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise).\n* Small size.\n\n## Table of contents\n\n* [Installation](#install)\n* [Usage](#usage)\n* [Examples](#examples)\n* [API](#api)\n* [Related projects](#related)\n* [Contributing](#contributing)\n* [License](#license)\n\n## Installation \u003ca name=\"install\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\n\n### Node\n\n    npm install povtor\n\n### [Bower](https://bower.io)\n\n    bower install povtor\n\n### AMD/UMD, \u0026lt;script\u0026gt;\n\nUse `dist/povtor.umd.js` (minified version).\n\n## Usage \u003ca name=\"usage\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\n\n### ECMAScript 6+\n\n```js\nimport {retry, getPromiseField} from 'povtor';\n```\n\n### Node\n\n```js\nconst retry = require('povtor').retry;\nconst getPromiseField = require('povtor').getPromiseField;\n```\n\n### AMD/UMD\n\n```js\ndefine(['path/to/dist/povtor.umd.js'], function(povtor) {\n    const retry = povtor.retry;\n    const getPromiseField = povtor.getPromiseField;\n});\n```\n\n### Bower, \u0026lt;script\u0026gt;\n\n```html\n\u003c!-- Use bower_components/povtor/dist/povtor.umd.js if the library was installed by Bower --\u003e\n\u003cscript type=\"text/javascript\" src=\"path/to/dist/povtor.umd.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\"\u003e\n    // povtor is available via povtor field of window object\n    const retry = povtor.retry;\n    const getPromiseField = povtor.getPromiseField;\n\u003c/script\u003e\n```\n\n## Examples \u003ca name=\"examples\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\n\n```js\n// Request data from API endpoint and repeat request after 3, then 5, then 7 seconds\n// when request is not successful, response data has error flag or value of state field is not 3.\nlet control = retry({\n    action: axios,\n    actionParams: [{url: '/api/some/endpoint'}],\n    retryAttempts: [3000, 5000, 7000],\n    retryTest: (response) =\u003e response.data.error || response.data.state !== 3,\n    retryOnError: true\n});\n\n// control.promise will be resolved when a request is completed successfully and retryTest returns false\n// or when all repeats will be made.\ncontrol.promise.then((response) =\u003e console.log(response.data));\n\ngetPromiseField(control);   // Get value of control.promise\n\n// Read file contents every second until it contains specific value.\ncontrol = retry({\n    action: jsonfile.readFileSync,\n    actionParams: ['/path/to/data.json', {'throws': false}],\n    retryTimeout: 1000,\n    retryTest: (data) =\u003e ! data || ! data.result\n});\n\n// control.promise will be resolved when the file contains specified value.\ncontrol.promise.then((data) =\u003e console.log(data.result));\n...\ncontrol.stop();   // Stop repeating of action\n```\n\nSee tests for additional examples.\n\n## API \u003ca name=\"api\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\n\n#### retry(settings: RetrySettings): RetryResult\n\nCall specified function and repeat calls depending on settings.\n\n**`settings: RetrySettings`** - an object that can contain the following fields:\n\n* `action` - a function that should be called. Can return a promise or a value.\n* `actionContext` - an object that should be used as `this` when calling the action function.\n* `actionParams` - an array of parameters that should be passed into the action function.\n* `delay` - an amount of milliseconds before first call of the action function.\n  When the value is not specified or is negative, the action function will be called immediately first time.\n* `retryAttempts` - an array specifying amount and timeouts between repeated calls of the action function.\n  Each item can be a number or a function (see `retryTimeout` setting for details).\n  Has priority over `retryQty` and `retryTimeout` settings.\n* `retryQty` - maximum number of repeated calls of the action function. A negative value means no restriction.\n  Default value is `-1`.\n* `retryTimeout` - a timeout between repeated calls of the action function, or a function that returns such timeout.\n  A negative or non-number value means the repeat call will be made without delay (this is applied by default).\n  If specified function returns `false` then retry process will be finished and result promise\n  will be fulfilled or rejected depending on result of the last action's call.\n* `retryOnError` - a boolean value or a function returning boolean value that specifies whether the action function\n  should be called again when the action function throws an error or returned promise is rejected.\n  When not specified the call of the action function will not be repeated on an error.\n* `retryTest` - a boolean value or a function returning boolean value that specifies whether the action function\n  should be called again after a made call. When not specified the action call will not be repeated.\n* `timeLimit` - time in milliseconds specifying how long retry process can last starting from call of `retry` function.\n\nThe only required field is `action`.\n\n`retry` function returns `RetryResult` - an object that can be used to observe and control the process of calls repeating.\n**`RetryResult`** contains the following fields:\n\n* `promise` - promise that can be used to observe the process and to get the final value of the action function\n  or the last error.\n* `attempt` - number of calls of the action function that have already made.\n* `error` - last error or value of promise rejection.\n* `isError` - whether the last call of the action function is ended with error.\n* `result` - contains result of each call of the action function.\n* `settings` - settings that were passed to `retry` function.\n* `startTime` - time in milliseconds when `retry` function was called.\n* `stop` - function that can be used to stop the process of calls repeating. Returns value of `promise` field.\n* `stopped` - a boolean value that indicates whether the process of calls repeating is stopped.\n* `value` - a value of last successfull call of the action function. When the action function returns a promise,\n  the value will be result of the promise fulfillment.\n* `valueWait` - a boolean value that indicates whether the action function is producing a result.\n  Useful only when the action function returns a promise. Is set to `true` when the promise is pending.\n* `wait` - a boolean value that indicates waiting of the next call of the action function.\n  Is set to `true` during a timeout between calls.\n\n#### getPromiseField(obj)\n\nReturn value of `promise` field of the passed object.\n\nSee `doc` folder for details.\n\n## Related projects \u003ca name=\"related\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\n\n* [eva](https://github.com/gamtiq/eva)\n* [wrapme](https://github.com/gamtiq/wrapme)\n\n## Contributing \u003ca name=\"contributing\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\nIn lieu of a formal styleguide, take care to maintain the existing coding style.\nAdd unit tests for any new or changed functionality.\nLint and test your code.\n\n## License \u003ca name=\"license\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\nCopyright (c) 2018-2020 Denis Sikuler\nLicensed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgamtiq%2Fpovtor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgamtiq%2Fpovtor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgamtiq%2Fpovtor/lists"}