{"name":"p-throttle","version":"8.0.0","keywords":["promise","throttle","throat","limit","limited","interval","rate","batch","ratelimit","queue","discard","async","await","promises","time","out","cancel","bluebird"],"author":{"url":"https://sindresorhus.com","name":"Sindre Sorhus","email":"sindresorhus@gmail.com"},"license":"MIT","_id":"p-throttle@8.0.0","maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"homepage":"https://github.com/sindresorhus/p-throttle#readme","bugs":{"url":"https://github.com/sindresorhus/p-throttle/issues"},"ava":{"serial":true},"dist":{"shasum":"0133d651c1390866beb6ee89903d93c6b9eba8fe","tarball":"http://registry-npm.ywwl.com/p-throttle/download/p-throttle-8.0.0.tgz","fileCount":5,"integrity":"sha512-kvpi14SZClZqNTLevyhCNQano1LH4clozDZoOdxnxyvEl17kjEKxkgD6to7mQMcWE4fMKAwbH0rLqm6Gjj7b2Q==","signatures":[{"sig":"MEQCIFmpmsOcKyepTF1Cjg6rvAhWl0fLTUB6GDUod0PoAL+gAiAs+g4zx2lluBwaM0r/YzZwJbu+Bk8nY7kxyM2MbCqqDQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":13802,"size":4560,"noattachment":false,"key":"/p-throttle/-/p-throttle-8.0.0.tgz"},"type":"module","types":"./index.d.ts","engines":{"node":">=20"},"exports":{"types":"./index.d.ts","default":"./index.js"},"funding":"https://github.com/sponsors/sindresorhus","gitHead":"46a8b5f5157305638b0610dd2ebd34a75f9322d8","scripts":{"test":"xo && ava && tsd","test-gc":"node --expose-gc gc-test"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"repository":{"url":"git+https://github.com/sindresorhus/p-throttle.git","type":"git"},"_npmVersion":"10.9.2","description":"Throttle promise-returning & async functions","directories":{},"sideEffects":false,"_nodeVersion":"20.19.1","_hasShrinkwrap":false,"devDependencies":{"xo":"^0.59.3","ava":"^6.2.0","tsd":"^0.31.2","delay":"^6.0.0","in-range":"^3.0.0","time-span":"^5.1.0","pretty-bytes":"^7.0.1"},"_npmOperationalInternal":{"tmp":"tmp/p-throttle_8.0.0_1755771364977_0.8900204682319819","host":"s3://npm-registry-packages-npm-production"},"readme":"# p-throttle\n\n> Throttle promise-returning & async functions\n\nAlso 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. All calls are queued and executed—the last call is guaranteed to run with its original context and arguments preserved.\n\n## Install\n\n```sh\nnpm install p-throttle\n```\n\n## Browser\n\nThis package works in the browser with modern browsers that support `WeakRef` and `FinalizationRegistry` (Chrome 84+, Firefox 79+, Safari 14.1+, Edge 84+).\n\n## Usage\n\nThis calls the function at most twice per 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##### signal\n\nType: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)\n\nAbort pending executions. When aborted, all unresolved promises are rejected with `signal.reason`.\n\n```js\nimport pThrottle from 'p-throttle';\n\nconst controller = new AbortController();\n\nconst throttle = pThrottle({\n\tlimit: 2,\n\tinterval: 1000,\n\tsignal: controller.signal\n});\n\nconst throttled = throttle(() => {\n\tconsole.log('Executing...');\n});\n\nawait throttled();\nawait throttled();\ncontroller.abort('aborted');\nawait throttled();\n//=> Executing...\n//=> Executing...\n//=> Promise rejected with reason `aborted`\n```\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##### weight\n\nType: `Function`\n\nCalculate the weight/cost of each function call based on its arguments.\n\nThe weight determines how much of the `limit` is consumed by each call. This is useful for rate limiting APIs that use point-based or cost-based limits, where different operations consume different amounts of the quota.\n\nBy default, each call has a weight of `1`.\n\nIn the following example, queries with different numbers of tables consume different amounts of the rate limit:\n\n```js\nimport pThrottle from 'p-throttle';\n\n// Storyblok GraphQL API: 100 points per second\n// Each query costs 1 point for the connection plus 1 point per table\nconst throttle = pThrottle({\n\tlimit: 100,\n\tinterval: 1000,\n\tweight: numberOfTables => 1 + numberOfTables\n});\n\nconst fetchData = throttle(numberOfTables => {\n\t// Fetch GraphQL data\n\treturn fetch('...');\n});\n\nawait fetchData(1); // Costs 2 points\nawait fetchData(3); // Costs 4 points\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.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\nThis can be useful for implementing queue management strategies, such as using a fallback when the queue is too full.\n\n```js\nimport pThrottle from 'p-throttle';\n\nconst throttle = pThrottle({limit: 1, interval: 1000});\n\nconst accurateData = throttle(() => fetch('https://accurate-api.example.com'));\nconst roughData = () => fetch('https://rough-api.example.com');\n\nasync function getData() {\n\tif (accurateData.queueSize >= 3) {\n\t\treturn roughData(); // Queue full, use fallback\n\t}\n\n\treturn accurateData();\n}\n```\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":1755771365193,"_cnpm_publish_time":1755771365193,"dist-tags":{"latest":"8.1.1"}}