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

Remove async/await that has no effect #275

Conversation

caillou
Copy link

@caillou caillou commented Oct 16, 2021

Description

Remove async/await that has no effect.

Performance impact

This should not change how the code runs.

Security impact

None.

Checklist

  • My code matches the project's code style and yarn lint:fix passes.
  • I've added tests for the new feature, and yarn test passes.
  • I have detailed the new feature in the relevant documentation.
  • I have added this feature to 'Pending' in the RELEASE_NOTES.md file (if one exists).
  • If this is a breaking change I've explained why.

@benjie
Copy link
Member

benjie commented Oct 17, 2021

Without the await:

  • errors thrown from the lookupOrganizationBySlug method will not be swallowed (you’ll get an unhandled promise rejection)
  • the setSlugCheckIsValid call will occur before the data is fetched which undermines the point of that variable

In general if an await occurs inside a try block it’s probably not safe to remove, even if it’s a return await ….

@benjie benjie closed this Oct 17, 2021
@caillou
Copy link
Author

caillou commented Oct 17, 2021

Ok, I might be completely wrong here, but lookupOrganizationBySlug does not return a Promise, but void.

There is no point awaiting it.

@caillou
Copy link
Author

caillou commented Oct 17, 2021

@benjie: So I took some more time to make sure my assumptions are right.

lookupOrganizationBySlug indeed returns void and there is no point awaiting it, nor does it make sense to wrap it within a try/catch block.

It is a LazyQuery. The potential error can be found in the slugError variable.

As of now setSlugCheckIsValid is always executed before lookupOrganizationBySlug returns. No matter if you await it or not. Calling setSlugCheckIsValid sets slugLoading to true, which is why the loading indicator remains.

I have a new commit that completely removes the try/catch block. Let me know if I may open a new PR with this: caillou@de26a6f

@benjie
Copy link
Member

benjie commented Oct 18, 2021

Ah I had forgot @singingwolfboy upgraded us to Apollo v3; in v2 this method returned a promise.

This code needs totally rewriting to work in Apollo v3 for the reasons listed above - removing the async/await would just hide that there's a bug here. The code needs to be changed such that there's a useEffect hook that calls setSlugCheckIsValid(true) when the networkStatus of the useOrganizationBySlugLazyQuery indicates that the load is complete (successful or otherwise), and the entire try/catch/finally needs removing. It would also be worth looking through the rest of the code for issues like this that were missed during the v2->v3 migration. A PR making these changes would be welcome 👍

@benjie
Copy link
Member

benjie commented Oct 18, 2021

While you're at it, turning the lint rule (I think it's @typescript-eslint/await-thenable) on that makes this kind of issue an error rather than a warning would be good so we don't hit this problem again in future migrations 😅

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