首页 时政热点 科技头条 智能AI 安全攻防 数码硬件 开发者生态 汽车 游戏 社会热点 开源推荐 医疗健康 归档 标签 关于

我为 TypeScript 制作了一个 Promise-aware debounce 和throttle 库

摘要

Small, strongly typed timing and concurrency utilities for modern TypeScript. Every scheduled call returns a real promise for the wrapped function's actual result. There are no runtime dependencies, a...

and search promise result temporize debounce for call CommonJS return
2026-08-02 1 阅读 约9分钟阅读 slimy74
分享:
字号:
适用于现代 TypeScript 的小型强类型计时和并发实用程序。每个计划的调用都会返回包装函数实际结果的真实承诺。没有运行时依赖性,并且该包以 ESM 和 CommonJS 形式提供。 npm install @alsoftworks/temporize 功能 temporize lodash.debounce / lodash.throttle 推断参数和解析的返回值 完整通用推断 单独提供的类型 每次调用返回 Promise> 最后同步结果或未定义 异步错误传播 本机 Promise 拒绝 调用者管理返回值 Cancellation .cancel() 和 AbortSignal .cancel() 最大去抖等待 是 是 Promise 队列速率限制器throttlePromise 否 异步重叠策略 debounceAsync 否 动画帧节流 浏览器 API 加节点回退 否 多调用参数批处理 否 指数回退重试 重试 否 Safari/Node 回退时的空闲周期调度 否 并发承诺限制 FIFO 队列的 concurrencyLimit 否 运行时依赖性 零 每个方法包的零 模块 ESM 和 CommonJS CommonJS 每个方法包 temporize 故意专注于控制工作运行的时间和频率。去抖、节流、批处理、重试定时、帧调度和空闲调度都属于该系列。对象、数组、字符串和其他通用实用程序助手故意超出范围;该边界是设计决定,而不是遗漏。从“@alsoftworks/temporize”导入 { debounce } ; const search=debounce(async(query:string)=>{constresponse=awaitfetch(`/api/search?q=${encodeURIComponent(query)}`);返回response.json()asPromise<{total:number}>;},250,{maxWait:1_000},); const result = wait search("类型推断");安慰 。日志(结果。总计);搜索 。待办的 ( ) ;等待搜索。冲洗();搜索 。取消 ( ) ;多个调用合并为一个调用,每个调用都会收到一个单独的承诺,该承诺由该调用的结果决定。仅前导去抖动解决了窗口内抑制的调用与前导调用结果的问题。从“@alsoftworks/temporize”导入{throttle}; const savePosition =throttle((x:number,y:number)=>({x,y,savedAt:Date.now()}),100,{leading:true,trailing:true},);常量保存 = 等待 savePosition ( 120 , 80 ) ;定期限制会合并多余的调用,并使用最新的参数进行尾随调用。从“@alsoftworks/temporize”导入{rafThrottle}; const updateLayout = rafThrottle ( ( width : number ) => { document . documentElement . style . setProperty ( "--viewport-width" , ` ${ width } px` ) ; } ) ;窗户 。 addEventListener ( "resize" , ( ) => updateLayout ( window .innerWidth ) ) ; // 丢弃已请求但尚未运行的帧。更新布局。取消 ( ) ;在浏览器中, rafThrottle 使用 requestAnimationFrame 。在 SSR 和 Node 中,它会回落到 16 毫秒 setTimeout ,因此当动画帧全局变量不存在时导入和调用它是安全的。从“@alsoftworks/temporize”导入{debounceAsync}; const loadUser = debounceAsync ( async ( id : string , signal : AbortSignal ) => { const response = wait fetch ( `/api/users/ ${ id } ` , { signal } ) ; 返回 response .json ( ) as Promise < { id : string ; name : string } > ; } , 200 , { 重叠 : “取消上一个” } , ) ; // AbortSignal 参数是内部提供的,在此调用中被省略。 const user =等待loadUser(“user_123”);重叠策略是: “队列”(默认):保留每个触发的调用并在活动工作解决后启动它。 "drop" : 不启动重叠调用;它的调用者采用主动调用的承诺。 “cancel-previous” :中止活动调用的内部提供的信号并立即开始新的调用。未安全声明信号的 JavaScript 函数会忽略额外的参数。对于类型化信号注入,声明所需的 AbortSignal 作为包装函数的最终参数。 debounceAsync 从返回函数的调用签名中删除该参数。取消是合作性的:异步操作必须观察信号才能停止其正在进行的工作。从“@alsoftworks/temporize”导入{throttlePromise}; const sendRequest =throttlePromise(async(path:string)=>fetch(path).then((response)=>response.status),100,); const requests = [ sendRequest("/api/one"), sendRequest("/api/two"), sendRequest("/api/三"),];安慰 。 log(sendRequest.queued());安慰 。 log(await Promise.all(请求));每个调用都会进入 FIFO 队列,并在上一次启动后至少启动一个窗口。队列限制调度率,而不是并发性:当下一个窗口开始时,慢速承诺可能仍然处于活动状态。从“@alsoftworks/temporize”导入{批次}; const markNotificationsRead=batch(async(calls:Array<[notificationId:string]>)=>{constids=calls.map(([notificat
这篇文章对您有帮助吗?

订阅66必读

每日精选科技资讯,直达你的邮箱