Skip to content

Commit

Permalink
fix: add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Falci committed Feb 21, 2024
1 parent dcd56e0 commit 48811d6
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ function App() {

const { refetch } = useQuery({
queryKey: [token, name],
queryFn: () =>
fetch(`/api?name=${name}&token=${token}`).then((r) => {
queryFn: () => {
const controller = new AbortController();
const { signal } = controller;

setTimeout(() => controller.abort(), 3000);

return fetch(`/api?name=${name}&token=${token}`, { signal }).then((r) => {
if (!r.ok) throw new Error('Network response was not ok');
return r.json();
}),
});
},
retry: 3,
retryDelay: 1,
enabled: false,
Expand All @@ -34,9 +40,13 @@ function App() {
const loadData = async () => {
if (!name) return;
setLoading(true);
const { data } = await refetch();
setAddr(data?.addr || '');
setLoading(false);
try {
const { data } = await refetch();
setAddr(data?.addr || '');
} finally {
setLoading(false);
}

window.history.pushState({}, '', `?name=${name}&token=${token}`);
};

Expand Down

0 comments on commit 48811d6

Please sign in to comment.