Skip to content

fix: reset debounce when request is not ready (#2581) - #2946

Merged
crazylxr merged 2 commits into
alibaba:masterfrom
jay666mnj:fix-use-request-ready-debounce-leading
Aug 5, 2026
Merged

fix: reset debounce when request is not ready (#2581)#2946
crazylxr merged 2 commits into
alibaba:masterfrom
jay666mnj:fix-use-request-ready-debounce-leading

Conversation

@jay666mnj

Copy link
Copy Markdown
Contributor

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

Fix #2581

💡 Background and solution

When useRequest is used with ready and debounceLeading, the pending debounce window may remain active after ready becomes false.

If ready turns back to true before that debounce window ends, the next auto run may not trigger immediately as expected by debounceLeading.

This PR cancels the current debounce window when ready becomes false, so the next ready=true auto run can trigger immediately with debounceLeading.

A test case is added for the ready + debounceLeading scenario.

📝 Changelog

Language Changelog
🇺🇸 English Fix useRequest may not trigger immediately when ready is used with debounceLeading.
🇨🇳 Chinese 修复 useRequest 同时使用 readydebounceLeading 时可能无法立即触发请求的问题。

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@pkg-pr-new

pkg-pr-new Bot commented Jul 17, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/ahooks@2946
npm i https://pkg.pr.new/@ahooks.js/use-url-state@2946

commit: 129893f

@jay666mnj

Copy link
Copy Markdown
Contributor Author

@tony @sorrycc request review please~

@crazylxr

Copy link
Copy Markdown
Collaborator

Thanks for the fix! The current change handles the reported case, but there is still one edge case.

After ready becomes false, calling run() while it remains false creates a new leading debounce window. Although the request itself is stopped by the ready plugin, switching ready back to true within debounceWait will still delay the next auto run.

Steps to reproduce:

  1. Start with ready: true and debounceLeading: true.
  2. Change ready to false.
  3. Call run() while ready is false.
  4. Change ready back to true before debounceWait expires.

The service is not called immediately in step 4.

Could we bypass debounce when fetchInstance.options.ready === false and add a regression test for this case? Once this edge case is covered, the fix should be good to merge.

@jay666mnj

Copy link
Copy Markdown
Contributor Author

Thanks for the fix! The current change handles the reported case, but there is still one edge case.

After ready becomes false, calling run() while it remains false creates a new leading debounce window. Although the request itself is stopped by the ready plugin, switching ready back to true within debounceWait will still delay the next auto run.

Steps to reproduce:

  1. Start with ready: true and debounceLeading: true.
  2. Change ready to false.
  3. Call run() while ready is false.
  4. Change ready back to true before debounceWait expires.

The service is not called immediately in step 4.

Could we bypass debounce when fetchInstance.options.ready === false and add a regression test for this case? Once this edge case is covered, the fix should be good to merge.

Thanks for pointing out this edge case.

I’ve updated the fix to bypass debounce when fetchInstance.options.ready === false, so calling run() while ready remains false will no longer create a new leading debounce window. I also updated the regression test to cover the exact flow you described: ready: true -> ready: false -> manual run() -> ready: true again within debounceWait.

Validation:

  • pnpm --filter ahooks test src/useRequest/__tests__/useDebouncePlugin.spec.ts
  • pnpm --filter ahooks test

Both passed locally.

@jay666mnj

Copy link
Copy Markdown
Contributor Author

@crazylxr

@crazylxr crazylxr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@crazylxr
crazylxr merged commit ba9dc60 into alibaba:master Aug 5, 2026
1 check passed
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.

useRequest的debounceLeading和ready结合使用时不生效

2 participants