Skip to content

Fetch middleware question #1

@hyeomans

Description

@hyeomans

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions