demo-projects/redux-patterns-demo

Fetch middleware question

Opened this issue · 0 comments

Hi,

I was wondering if the Fetch api could unintentionally releasing the Zalgo

  if(action.type === API_REQUEST) {
    const { method, url, onSuccess, onError } = action.meta;

    fetch(url, { method }).then(response => response.json())
      .then((data) => dispatch({ type: onSuccess, payload: data }))
      .catch(error => dispatch({ type: onError, payload: error }))
  }
  return next(action)

What do you think of changing it to:

  if(action.type === API_REQUEST) {
    const { method, url, onSuccess, onError } = action.meta;

    return fetch(url, { method }).then(response => response.json())
      .then((data) => {
        dispatch({ type: onSuccess, payload: data });
        return next(action);
     });
      .catch(error => dispatch({ type: onError, payload: error }))
  }
  return Promise.resolve(next(action));

At the end next(action) is always executed async but the flow will continue normally.