Skip to content

Commit

Permalink
Fixed setValue in useAsync hook
Browse files Browse the repository at this point in the history
  • Loading branch information
puehringer committed Apr 4, 2022
1 parent e0bc264 commit 1af07aa
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/hooks/useAsync.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion dist/hooks/useAsync.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/hooks/useAsync.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/hooks/useAsync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export const useAsync = <F extends (...args: any[]) => any, E = Error, T = Await
const execute = React.useCallback(
(...args: Parameters<typeof asyncFunction>) => {
setStatus('pending');
setValue(null);
// Do not unset the value, as we mostly want to retain the last value to avoid flickering, i.e. for "silent" updates.
// setValue(null);
setError(null);
const currentPromise = Promise.resolve(asyncFunction(...args))
.then((response: T) => {
Expand All @@ -58,6 +59,7 @@ export const useAsync = <F extends (...args: any[]) => any, E = Error, T = Await
})
.catch((e: E) => {
if (currentPromise === latestPromiseRef.current) {
setValue(null);
setError(e);
setStatus('error');
}
Expand Down

0 comments on commit 1af07aa

Please sign in to comment.