I am using API call to get data and update my redux store. Following is state changes for each API call.
- Before making API call, set
isLoading
astrue
- On success reset
isLoading
asfalse
- On failure reset
isLoading
asfalse
and setisError
astrue
.
My component needs data from three APIs which is stored in three different redux stores as below structure.
store: {
book: {
isLoading: false,
isError: false,
data: {}
},
teacher: {
isLoading: false,
isError: false,
data: {}
},
}
In my component, I use following to call api
componentWillMount() {
const {
loadBook,
loadTeacher,
} = this.props;
// all these load functions dispatch action which returns Promise for async API call using `fetch`
const apiCalls = [
loadBook(),
loadTeacher(),
];
Promise.all(apiCalls);
}
I have written selector
to check the loading state as below.
export const getIsLoading = createSelector([
getIsBookLoading,
getIsTeacherLoading,
],
(bLoading, tLoading) => (
bLoading || tLoading
)
);
Based on value of getIsLoading
I do show loading state in my component otherwise render component.
However I see problem happens when one of the API call fails. For example, loadBook
fails in 100 ms and that time bLoading
is changed back to false
however tLoading
still is true bcz loadTeacher
api calls was not finished. Since Promise.all()
do either all or nothing therefore API call for loadTeacher
never finished therefore tLoading
stas true
.
Is there a way to let Promsie.all
to resolve all the calls even if some failed so that it can clean dirty state?