@ambassify/throttle

中文文档
3.0.2发布于 2025/04/01MITCommonJS近 30 天下载 3835

Throttle depending on function arguments.

原始使用文档
文档目录9 节
  1. Installation
  2. Usage
  3. ThrottleOptions : Object
  4. ThrottledFunction : function
  5. CacheItem
  6. Example
  7. Contributing
  8. License
  9. About us

Throttle

CircleCI

Create a throttled version of a function.

Installation

npm install --save @ambassify/throttle

Usage

Creates a throttled version of func. func will only be invoked when its result is not in the throttled function's cache or the time between the current and last invocation is larger than what's specified by delay.

If func is async, the throttled function will immediately return a value from cache (if available) while func is executing.

Returns: ThrottledFunction - The throttled version of "func"

ParamTypeDescription
funcfunctionThe function to throttle
optionsThrottleOptions

Example

const throttle = require('@ambassify/throttle');

const throttled = throttle(<function-to-throttle>, <options>);

throttled('hello');
throttled.clear(<...args>);

ThrottleOptions : Object

NameTypeDescription
delaynumberHow much time must have been elapsed for func to be invoked again when there's a chached result available. Defaults to Infinity.
maxAgenumberHow long are items allowed to remain in cache. Unlimited by default.
maxSizenumberHow long are items allowed to remain in cache. Unlimited by default.
cacheMapSpecify a custom cache for throttle to use. Must provide methods that match Map's equivalent: has, get, set, delete, clear
resolverfunctionGiven the same arguments used to invoke func return only what's important to build the cache key.
onUpdatedfunctionInvoked with the cacheItem whenever the item is updated.
onError'clear' | 'cached' | 'persist' | functionError handler to use when "func" throws or rejects. - clear: The cache is cleared and the error is thrown - persist: The error is saved into cache and thrown - cached: If a previous value is in cache, it will be returned, if not the error will be thrown - a custom function that receives the error as first param and cacheItem as the second, when specified the throttled function won't touch the cache when an error occurs, it's up to this handler to interact with cacheItem.

ThrottledFunction : function

The throttled function.

Properties

NameTypeDescription
clearfunctionWhen invoked without any arguments the entire cache is cleared, when are supplied they are passed, the item for those arguments is removed from cache.

CacheItem

CacheItems are exposed through the onUpdated callback that can be specified in the throttle options.

  • cacheItem.initialized: Whether or not the item has its initial value
  • cacheItem.pending: The pending promise when throttle is currently running but already has a previous value
  • cacheItem.key: The key for this cache item
  • cacheItem.stale: Whether or not the cache item's value is considered stale based on the last time it was updated and its "delay" option.
  • cacheItem.value: Current value of the cache item. When this is set, all timers and the like for the item are also reset
  • cacheItem.error: Same as "value" but used to indicate the result of throttled is an error
  • cacheItem.clear(): Clear the item from throttled's cache
  • cacheItem.delay(delay): Update this specific item's "delay"
  • cacheItem.maxAge(maxAge): Update this specific item's "maxAge"

Example

const throttle = require('@ambassify/throttle');

let example = 0;

async function myFunction(input) {
    // Delay 500 ms to fake a slow operation
    await new Promise(resolve => setTimeout(resolve, 500));

    example += input;
    return example;
}

// Allow `myFunction` to be called once every 2 seconds for each different
//`input` and cache the value for max 5 seconds.
const throttled = throttle(myFunction, { delay: 2000, maxAge: 5000 });

throttled(1); // 1
throttled(1); // 1
throttled(1); // 1

// Wait for 2 seconds
// "myFunction" is called, but the old cached result is returned immediately

throttled(1); // 1

// Wait 500ms (the fake delay)
// "myFunction" isn't called (as delay hasn't been reached) but we do get the
// latest result

throttled(1); // 2

// Wait for 5 seconds
// "myFunction" is called but the old cached value has expired so it resolves
// with the new value once the function has run

throttled(1); // 3

const conditional = throttle(myFunction, {
    delay: 2000,
    maxAge: 5000
    onCached: function(item) {
        // Only cache results for large values of input
        if (item.key < 10)
            item.clear();
    }
}

conditional(1); // 1
conditional(1); // 2
conditional(1); // 3

conditional(20); // 23
conditional(20); // 23

Contributing

If you have some issue or code you would like to add, feel free to open a Pull Request or Issue and we will look into it as soon as we can.

License

We are releasing this under a MIT License.

About us

If you would like to know more about us, be sure to have a look at our website, or our Twitter accounts @Ambassify, Sitebase, JorgenEvens.

版本与发布记录

@ambassify/throttle 共发布 16 个版本,首次发布于 2017/02/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.2latest2025/04/011.6 年前
3.0.12024/08/082 年前
3.0.02024/08/072 年前
2.0.02022/05/054 年前
1.0.12020/05/056 年前
1.0.02020/05/056 年前

关于 @ambassify/throttle 的常见问题

@ambassify/throttle 是什么?

Throttle depending on function arguments.

如何安装 @ambassify/throttle?

在项目目录运行 npm install @ambassify/throttle,或使用 pnpm add / yarn add / bun add。

@ambassify/throttle 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ambassify__throttle 是否存在。

@ambassify/throttle 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ambassify/throttle 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ambassify/throttle 可以商用吗?

@ambassify/throttle 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ambassify/throttle 的下载量有多少?

根据 npm 官方下载统计,@ambassify/throttle 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,835 次,日均约 128 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ambassify/throttle 还在维护吗?

最新版本 3.0.2 发布于 2025/04/01(1.6 年前);至今共发布 16 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/02/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。