{"name":"async-retry-ng","version":"2.0.2","description":"Retrying made simple, easy and async","main":"./lib/index.js","scripts":{"test":"jest --testTimeout 20000","prettier":"prettier --single-quote --write './{src,__tests__}/**/*.js'"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/turist-cloud/async-retry-ng.git"},"devDependencies":{"jest":"29.7.0","node-fetch":"2.6.7","prettier":"3.4.2","then-sleep":"1.0.1"},"_id":"async-retry-ng@2.0.2","gitHead":"4826740c02d684f9c7343b712f6c280834e16dbd","types":"./lib/index.d.ts","bugs":{"url":"https://github.com/turist-cloud/async-retry-ng/issues"},"homepage":"https://github.com/turist-cloud/async-retry-ng#readme","_nodeVersion":"20.11.1","_npmVersion":"10.2.4","dist":{"integrity":"sha512-mXb0C8K7sgQHwmohKeQUWtXR/VHVvp9rBGtqclMcLVWtz/8gxNKLkOXEP5wjDMsDGGXxdRPrV/Z4349MbAOSxA==","shasum":"c46db8fc6f67b75079c373381e64bfea53a0f298","tarball":"http://registry-npm.ywwl.com/async-retry-ng/download/async-retry-ng-2.0.2.tgz","fileCount":7,"unpackedSize":12462,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDSG+pE78wT/TPDt6m4+mMoNRAcc6EwkOiXfs3NYegVnAIhANylX9/ocSSvuX8dnd17fFVMm+OBpr/xL7fgB2wf1RCJ"}],"size":4977,"noattachment":false,"key":"/async-retry-ng/-/async-retry-ng-2.0.2.tgz"},"_npmUser":{"name":"olliv","email":"olli.vanhoja@gmail.com"},"directories":{},"maintainers":[{"name":"olliv","email":"olli.vanhoja@gmail.com"},{"name":"jamo","email":"jamo@isotalo.fi"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/async-retry-ng_2.0.2_1735637725848_0.0012143795184662576"},"_hasShrinkwrap":false,"readme":"# async-retry-ng\n\n[![Code Style](https://badgen.net/badge/code%20style/airbnb/fd5c63)](https://github.com/airbnb/javascript)\n\nRetrying made simple, easy, and async.\n\n## Usage\n\n```js\n// Packages\nconst retry = require('async-retry-ng')\nconst fetch = require('node-fetch')\n\nawait retry(async bail => {\n  // if anything throws, we retry\n  const res = await fetch('https://google.com')\n\n  if (403 === res.status) {\n    // don't retry upon 403\n    bail(new Error('Unauthorized'))\n    return\n  }\n\n  const data = await res.text()\n  return data.substr(0, 500)\n}, {\n  retries: 5\n})\n```\n\n### API\n\n```js\nretry(retrier : Function, opts : Object) => Promise\n```\n\n- The supplied function can be `async` or not. In other words, it can be a function that returns a `Promise` or a value.\n- The supplied function receives two parameters\n  1. A `Function` you can invoke to abort the retrying (bail)\n  2. A `Number` identifying the attempt. The absolute first attempt (before any retries) is `1`.\n- The `opts`\n  * `retries`: The maximum amount of times to retry the operation. Default is `10`.\n  * `factor`: The exponential factor to use. Default is `2`.\n  * `minTimeout`: The number of milliseconds before starting the first retry. Default is `1000`.\n  * `maxTimeout`: The maximum number of milliseconds between two retries. Default is `Infinity`.\n  * `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `true`.\n  * `onRetry`: an optional `Function` that is invoked after a new retry is performed. It's passed the `Error` that triggered it as a parameter.\n\nAll time values are in milliseconds.\n\n### Retries Explained\n\nThe formula used to calculate the individual timeouts is:\n\n```\nMath.min(random * minTimeout * Math.pow(factor, attempt), maxTimeout)\n```\n\nHave a look at [this article][article] for a better explanation of approach.\n\nIf you want to tune your `factor` / `times` settings to attempt the last retry\nafter a certain amount of time, you can use wolfram alpha. For example in order\nto tune for `10` attempts in `5 minutes`, you can use this equation:\n\n![screenshot](https://github.com/OlliV/async-retry-ng/raw/master/equation.gif)\n\nExplaining the various values from left to right:\n\n* `k = 0 ... 9`:  The `retries` value (10)\n* `1000`: The `minTimeout` value in ms (1000)\n* `x^k`: No need to change this, `x` will be your resulting factor\n* `5 * 60 * 1000`: The desired total amount of time for retrying in ms (5 minutes)\n\nTo make this a little easier for you, use wolfram alpha to do the calculations:\n\n<http://www.wolframalpha.com/input/?i=Sum%5B1000*x^k%2C+{k%2C+0%2C+9}%5D+%3D+5+*+60+*+1000>\n\n[article]: http://dthain.blogspot.com/2009/02/exponential-backoff-in-distributed.html\n","publish_time":1735637726027,"_cnpm_publish_time":1735637726027,"dist-tags":{"latest":"2.0.2"}}