Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete unnecessary return new Promise in Redux actions. #2321

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

lindapaiste
Copy link
Collaborator

Changes:

  • Remove all unnecessary new Promise constructors from Redux actions.

We have a lot of instances where we a wrapping an API call in return new Promise((resolve, reject) => {. This is unnecessary because the API call is already a Promise.

This code:

return new Promise((resolve) => {
  someFunction(args)
    .then((response) => {
      /** ... **/
      resolve();
    })
    .catch((error) => {
      /** ... **/
      resolve({ error });
    });
});

Is the same as this:

return someFunction(args)
  .then((response) => {
    /** ... **/
  })
  .catch((error) => {
    /** ... **/
    return { error };
  });

Both return a Promise which either resolves to void (on success) or resolves to an object with an error property (on failure).

There's a lot more cleanup that can be done here in the future, but right now I'm just changing this one thing. Notes for the future.

  • We often resolve to { error } but we don't use this value anywhere.
    -I've encountered uncaught errors which are thrown in the catch clauses. We are assuming that the error variable always has a property error.response.data and this is not always the case, as the catch clause catches ALL errors. (This won't crash the app but it logs to the console).
  • Some async action creator functions return the Promise and others don't.

This returns a Promise:

return (dispatch) =>
  apiClient
    .get('path')

This returns void:

return (dispatch) => {
  apiClient
    .get('path')
};

I have verified that this pull request:

  • has no linting errors (npm run lint)
  • has no test errors (npm run test)
  • is from a uniquely-named feature branch and is up to date with the develop branch.
  • is descriptively named and links to an issue number, i.e. Fixes #123

Copy link
Contributor

@PiyushChandra17 PiyushChandra17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think return is missing, otherwise all the refactor looks good to me. Nicely done.

resolve({ error });
});
});
submitFile(formProps, files, parentId, projectId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think return is missing here, otherwise all the changes looks good to me. Nicely Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants