Making JSON requests to fetch relationships asynchronously I would like to refactor the fetchRelationships function to use async await. I am not sure what the best way is to do it as this code contains nested .then at response.json().then((json) =&gt;....

Could sby pls post the refactored version?

export const fetchRelationshipsError = (error) =&gt; {
  return {
    type: FETCH_RELATIONSHIPS_FAILURE,
    payload: { error }
  };
};

export const fetchRelationshipsRequest = () =&gt; {
  return { type: FETCH_RELATIONSHIPS_REQUEST };
};

export const fetchRelationshipsSuccess = (relationships) =&gt; {
  return {
    type: FETCH_RELATIONSHIPS_SUCCESS,
    payload: relationships
  };
};

export const fetchRelationships = (url) =&gt; {
  return (dispatch) =&gt; {
    dispatch(fetchRelationshipsRequest());

    fetch(url, config)
    .then((response) =&gt; {
      const responseObj = {
        response: response,
        payload: response.json().then((json) =&gt; {
          return { body: json }
        })
      };

      if (!response.ok) {
        const error = new Error(response.statusText);
        responseObj.payload.then((response) =&gt; {
          show_error_alert(response.body);
          dispatch(fetchRelationshipsError(response.body));
        });
        throw error;
      }

      return responseObj.payload;
    })
    .then((response) =&gt; {
      dispatch(fetchRelationshipsSuccess(response.body))
    })
    .catch((error) =&gt; { console.log('Request failed', error); });
  };
};
First, I would get that error check out of the way instead of in the middle of the operation. Just throw an error in the middle of the operation and let the catch handle the dispatch.

Also, fetch will always resolve. A common procedure is to use the very first then to evaluate the response and determine how the sequence proceeds. It's usually either resolving with the data or rejecting with the error. But in your case, since both success and error actions dispatch body, the only difference is the action creator. Instead of a resolution/rejection, we just resolve with the correct action based on ok.

export const fetchRelationships = url =&gt; {
  return async dispatch =&gt; {
    dispatch(fetchRelationshipsRequest())

    fetch(url, config)
      .then(res =&gt;
        // We cannot return res.json() and do its then as part of the
        // outer sequence since we need res. This needs to nest.
        res.json().then(body =&gt; 
          res.ok ? fetchRelationshipsSuccess(body) : fetchRelationshipsError(body)))
      .then(action =&gt; dispatch(action))
      .catch((error) =&gt; dispatch(genericErrorAction())
  }
}


Now for async-await. The returned anonymous function from fetchRelationships will have to be async in order for its body to be able to use await. All promise-returning functions will need to use await. And then turn everything into synchronous-looking code. Here's the same procedure as above, but in async-await form.

export const fetchRelationships = url =&gt; {
  return dispatch =&gt; {
    dispatch(fetchRelationshipsRequest())

    try {
      const res = await fetch(url, config)
      const body = await res.json()
      const action = res.ok ? fetchRelationshipsSuccess(body) : fetchRelationshipsError(body)
      dispatch(action)
    } catch(error){
      dispatch(genericErrorAction())
    }
  }
}