hooks
hooks copied to clipboard
fix(useRequest): return immediately in runAsync when ready=false (no pending)
🤔 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, whenready=false, callingrunAsyncreturns a Promise that stays pending, causing the call to hang.
- In
- Solution:
- In
Fetch.runAsync, when a plugin signalsstopNow, returnPromise.resolve(state.data)instead of a never-resolving Promise. - Add tests to ensure that when
ready=false(in both manual and auto modes),runAsyncresolves immediately and does not remain pending.
- In
📝 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
npm i https://pkg.pr.new/ahooks@2866
npm i https://pkg.pr.new/@ahooksjs/use-url-state@2866
commit: e443b02