Created
January 15, 2024 01:07
-
-
Save geekelo/28164f160b9bf9c4224ed384263decb0 to your computer and use it in GitHub Desktop.
Using promise.all in dispatching & checking results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
useEffect(() => { | |
const fetchData = async () => { | |
try { | |
// Use Promise.allSettled to ensure that all promises are settled, | |
// regardless of success or failure. | |
const results = await Promise.allSettled([ | |
dispatch(displayCertificates()), | |
dispatch(displayStudents()), | |
dispatch(displayPersonnel()), | |
]); | |
console.log(results); | |
// Check if all promises were fulfilled successfully | |
const allFulfilled = results.every((result) => result.status === 'fulfilled'); | |
console.log(allFulfilled); | |
if (allFulfilled) { | |
console.log(certificates); | |
console.log(students); | |
const id = location.pathname.split('/').pop(); | |
setStudentId(id); | |
searchcert(); | |
} else { | |
// Handle the case where any of the promises was rejected. | |
console.error('One or more actions failed.'); | |
} | |
} catch (error) { | |
console.error('Error fetching data:', error); | |
} | |
}; | |
fetchData(); | |
// eslint-disable-next-line react-hooks/exhaustive-deps | |
}, [dispatch, location.pathname, certi]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment