Skip to content

Instantly share code, notes, and snippets.

@geekelo
Created January 15, 2024 01:07
Show Gist options
  • Save geekelo/28164f160b9bf9c4224ed384263decb0 to your computer and use it in GitHub Desktop.
Save geekelo/28164f160b9bf9c4224ed384263decb0 to your computer and use it in GitHub Desktop.
Using promise.all in dispatching & checking results
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