{"name":"p-throttle","version":"6.2.0","description":"Throttle promise-returning & async functions","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/p-throttle.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./index.d.ts","default":"./index.js"},"sideEffects":false,"engines":{"node":">=18"},"scripts":{"test":"xo && ava && tsd"},"keywords":["promise","throttle","throat","limit","limited","interval","rate","batch","ratelimit","queue","discard","async","await","promises","time","out","cancel","bluebird"],"devDependencies":{"ava":"^5.3.1","delay":"^6.0.0","in-range":"^3.0.0","time-span":"^5.1.0","tsd":"^0.29.0","xo":"^0.56.0"},"ava":{"serial":true},"_id":"p-throttle@6.2.0","gitHead":"31db2ba7146f9bdb625df7d393b54e4ab8781454","types":"./index.d.ts","bugs":{"url":"https://github.com/sindresorhus/p-throttle/issues"},"homepage":"https://github.com/sindresorhus/p-throttle#readme","_nodeVersion":"18.20.2","_npmVersion":"10.6.0","dist":{"integrity":"sha512-NCKkOVj6PZa6NiTmfvGilDdf6vO1rFCD3KDnkHko8dTOtkpk4cSR/VTAhhLMG9aiQ7/A9HYgEDNmxzf6hxzR3g==","shasum":"bc75ff1f128680bec15568cbf7312ae34a9d35ed","tarball":"http://registry-npm.ywwl.com/p-throttle/download/p-throttle-6.2.0.tgz","fileCount":5,"unpackedSize":10823,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCDhn/dYZrQi1anR9EArm9hySFwsnvlITY1DGd2jXxj1gIhAODXE8zUz/7CiXN4Zy9VoQkZzJUAj/Wb1LTUy1FpZG97"}],"size":3801,"noattachment":false,"key":"/p-throttle/-/p-throttle-6.2.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/p-throttle_6.2.0_1723645405899_0.2002418747774637"},"_hasShrinkwrap":false,"readme":"# p-throttle\n\n> Throttle promise-returning & async functions\n\nIt also works with normal functions.\n\nIt rate-limits function calls without discarding them, making it ideal for external API interactions where avoiding call loss is crucial.\n\n## Install\n\n```sh\nnpm install p-throttle\n```\n\n## Usage\n\nHere, the throttled function is only called twice a second:\n\n```js\nimport pThrottle from 'p-throttle';\n\nconst now = Date.now();\n\nconst throttle = pThrottle({\n\tlimit: 2,\n\tinterval: 1000\n});\n\nconst throttled = throttle(async index => {\n\tconst secDiff = ((Date.now() - now) / 1000).toFixed();\n\treturn `${index}: ${secDiff}s`;\n});\n\nfor (let index = 1; index <= 6; index++) {\n\t(async () => {\n\t\tconsole.log(await throttled(index));\n\t})();\n}\n//=> 1: 0s\n//=> 2: 0s\n//=> 3: 1s\n//=> 4: 1s\n//=> 5: 2s\n//=> 6: 2s\n```\n\n## API\n\n### pThrottle(options)\n\nReturns a throttle function.\n\n#### options\n\nType: `object`\n\nBoth the `limit` and `interval` options must be specified.\n\n##### limit\n\nType: `number`\n\nThe maximum number of calls within an `interval`.\n\n##### interval\n\nType: `number`\n\nThe timespan for `limit` in milliseconds.\n\n##### strict\n\nType: `boolean`\\\nDefault: `false`\n\nUse a strict, more resource intensive, throttling algorithm. The default algorithm uses a windowed approach that will work correctly in most cases, limiting the total number of calls at the specified limit per interval window. The strict algorithm throttles each call individually, ensuring the limit is not exceeded for any interval.\n\n##### onDelay\n\nType: `Function`\n\nGet notified when function calls are delayed due to exceeding the `limit` of allowed calls within the given `interval`. The delayed call arguments are passed to the `onDelay` callback.\n\nCan be useful for monitoring the throttling efficiency.\n\nIn the following example, the third call gets delayed and triggers the `onDelay` callback:\n\n```js\nimport pThrottle from 'p-throttle';\n\nconst throttle = pThrottle({\n\tlimit: 2,\n\tinterval: 1000,\n\tonDelay: (a, b) => {\n\t\tconsole.log(`Reached interval limit, call is delayed for ${a} ${b}`);\n\t},\n});\n\nconst throttled = throttle((a, b) => {\n\tconsole.log(`Executing with ${a} ${b}...`);\n});\n\nawait throttled(1, 2);\nawait throttled(3, 4);\nawait throttled(5, 6);\n//=> Executing with 1 2...\n//=> Executing with 3 4...\n//=> Reached interval limit, call is delayed for 5 6\n//=> Executing with 5 6...\n```\n\n### throttle(function_)\n\nReturns a throttled version of `function_`.\n\n#### function_\n\nType: `Function`\n\nA promise-returning/async function or a normal function.\n\n### throttledFn.abort()\n\nAbort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.\n\n### throttledFn.isEnabled\n\nType: `boolean`\\\nDefault: `true`\n\nWhether future function calls should be throttled and count towards throttling thresholds.\n\n### throttledFn.queueSize\n\nType: `number`\n\nThe number of queued items waiting to be executed.\n\n## Related\n\n- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions\n- [p-limit](https://github.com/sindresorhus/p-limit) - Run multiple promise-returning & async functions with limited concurrency\n- [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions\n- [More…](https://github.com/sindresorhus/promise-fun)\n","publish_time":1723645406057,"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"_cnpm_publish_time":1723645406057,"dist-tags":{"latest":"8.1.1"}}