-
Notifications
You must be signed in to change notification settings - Fork 0
/
redux-parallel-data-fetch.js
26 lines (20 loc) · 1.01 KB
/
redux-parallel-data-fetch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// This technique allows parallel data fetching from multiples sources in a component, with individual error handling. Since the individual promises always resolve, Promise.all will never fail (it fails if even one promise rejects). Errors can then be handled on an individual basis in Redux.
class SomeComponent extends React.Component {
componentDidMount() {
const promises = this.props.dataUrlsArray.map(this.getData)
Promise.all(promises);
}
getData() {
// promise resolves no matter what so Promise.all above never fails, even with an error
// (allows individual error handling)
return new Promise((resolve, reject) =>
this.props.operations.getDataThunk().then(resolve));
}
render() {
const { data, errorMessage, isLoading } = this.props;
if (isLoading) // handle loading state
if (errorMessage) // handle error state
// handle data
}
}
// Redux state could also look something like { source1: { data, errorMessage, isLoading }, source2: { data, errorMessage, isLoading } }