Skip to content

Commit

Permalink
add catch error
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliafito committed Mar 2, 2025
1 parent 3c875ea commit 8ea657d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
27 changes: 24 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,36 @@ export const App: React.FC = () => {
const [goods, setGoods] = useState<Good[]>([]);

const loadAllGoods = () => {
getAll().then(setGoods);
return getAll()
.then(setGoods)
.catch(error => {
// eslint-disable-next-line no-console
console.error('Failed to fetch goods:', error);

return [];
});
};

const load5FirstGoods = () => {
get5First().then(setGoods);
return get5First()
.then(setGoods)
.catch(error => {
// eslint-disable-next-line no-console
console.error('Failed to fetch the first 5 goods:', error);

return [];
});
};

const loadRedGoods = () => {
getRedGoods().then(setGoods);
return getRedGoods()
.then(setGoods)
.catch(error => {
// eslint-disable-next-line no-console
console.error('Failed to fetch red goods:', error);

return [];
});
};

return (
Expand Down
12 changes: 7 additions & 5 deletions src/api/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export function getAll(): Promise<Good[]> {
}

export const get5First = () => {
return getAll().then(goods =>
goods
.sort((good1, good2) => good1.name.localeCompare(good2.name))
.slice(0, 5),
);
return getAll()
.then(goods =>
goods.sort((a, b) => {
return a.name.localeCompare(b.name);
}),
)
.then(sortedGoods => sortedGoods.slice(0, 5));
};

export const getRedGoods = () => {
Expand Down

0 comments on commit 8ea657d

Please sign in to comment.