Skip to content

Commit

Permalink
Prevent callback hell in usePopup by using await
Browse files Browse the repository at this point in the history
  • Loading branch information
juskek committed Jul 6, 2023
1 parent 7b20da3 commit 961dfa4
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/usePopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,25 @@ export const usePopup = () => {
}, [selectedCountries, averageSpecificEmissions, calculationsRepository]);

useMountEffect(() => {
selectedCountriesRepository
.getSelectedCountriesAndPercentages()
.then((newMap) => {
setSelectedCountries(newMap);
});
const getSelectedCountriesAndSetState = async () => {
const newMap =
await selectedCountriesRepository.getSelectedCountriesAndPercentages();
setSelectedCountries(newMap);
};
getSelectedCountriesAndSetState();
});

useMountEffect(() => {
calculationsRepository.getLastCalculation().then((calculationData) => {
const getLastCalculationAndSetState = async () => {
const calculationData =
await calculationsRepository.getLastCalculation();
settotalBytesTransferred(calculationData?.bytes ?? 0);
setEmissions(calculationData?.emissions ?? 0);
setAverageSpecificEmissions(
calculationData?.specificEmissions ?? 0
);
});
};
getLastCalculationAndSetState();
});

return {
Expand Down

0 comments on commit 961dfa4

Please sign in to comment.