Skip to content

Conversation

@Arktomson
Copy link
Contributor

@Arktomson Arktomson commented Jan 25, 2026

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

Fixes #2898

💡 需求背景和解决方案

问题描述:

当使用 useRequest 配置节流选项(throttleLeading: true, throttleTrailing: false)时,runAsync 方法不遵守节流配置,会发送所有请求;而 run 方法能够正常工作。

问题根因:

问题由 useRequestImplement.ts 中使用 .bind() 方法导致。当调用 fetchInstance.runAsync.bind(fetchInstance) 时,会创建一个新函数,该函数永久引用绑定时刻的原始 runAsync 方法。之后,当节流插件替换 fetchInstance.runAsync 为节流版本时,已绑定的函数仍然指向原始的未节流方法。

解决方案:

runAsync 的绑定方式从:

runAsync: useMemoizedFn(fetchInstance.runAsync.bind(fetchInstance))

改为:

runAsync: useMemoizedFn((...args: TParams) => fetchInstance.runAsync(...args))

- Changed runAsync binding from .bind() to arrow function for dynamic lookup
- Added unit tests for runAsync with throttle configuration
- Fixes alibaba#2898
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 25, 2026

npm i https://pkg.pr.new/ahooks@2899
npm i https://pkg.pr.new/@ahooksjs/use-url-state@2899

commit: d788eab

@meet-student meet-student requested a review from crazylxr January 25, 2026 15:34
@whatwg6
Copy link

whatwg6 commented Jan 27, 2026

本地 patch 测试了下。节流虽然生效了,但是 2 个 promise 只有一个 resolve,另一个永远不会 resolve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug useRequest] runAsync does not respect throttle options while run works correctly

2 participants