1export function fetchProducts() {
2 return dispatch => {
3 dispatch(fetchProductsBegin());
4 return fetch("/products")
5 .then(handleErrors)
6 .then(res => res.json())
7 .then(json => {
8 dispatch(fetchProductsSuccess(json.products));
9 return json.products;
10 })
11 .catch(error => dispatch(fetchProductsFailure(error)));
12 };
13}
14
15// Handle HTTP errors since fetch won't.
16function handleErrors(response) {
17 if (!response.ok) {
18 throw Error(response.statusText);
19 }
20 return response;
21}
22