hooks icon indicating copy to clipboard operation
hooks copied to clipboard

fix(useRequest): return immediately in runAsync when ready=false (no pending)

Open lvboda opened this issue 2 months ago • 2 comments

[中文版模板 / Chinese template]

🤔 This is a ...

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

🔗 Related issue link

Close #2792

💡 Background and solution

  • Background:
    • In useRequest, when ready=false, calling runAsync returns a Promise that stays pending, causing the call to hang.
  • Solution:
    • In Fetch.runAsync, when a plugin signals stopNow, return Promise.resolve(state.data) instead of a never-resolving Promise.
    • Add tests to ensure that when ready=false (in both manual and auto modes), runAsync resolves immediately and does not remain pending.

📝 Changelog

function Component() {
  const { runAsync } = useRequest(fetchUser, { manual: true, ready: false });

  const onSave = async () => {
    const res = await runAsync({ id: 1 });
    // Before: this code never ran because the await above blocked indefinitely
    // After: written like this, it throws:
    // Uncaught TypeError: Cannot read properties of undefined (reading 'user')
    alert(res.user);
  };

  return <button onClick={onSave}>Save</button>;
}
Language Changelog
🇺🇸 English fix(useRequest): return immediately in runAsync when ready=false (no pending)
🇨🇳 Chinese fix(useRequest): 当 ready=false 时调用 runAsync 立即返回而不是阻塞

☑️ Self Check before Merge

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

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

lvboda avatar Oct 24 '25 07:10 lvboda

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Oct 24 '25 07:10 CLAassistant

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

commit: e443b02

pkg-pr-new[bot] avatar Oct 27 '25 05:10 pkg-pr-new[bot]