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

add eslint rules for promises #3796

Closed
wants to merge 16 commits into from
Closed

Conversation

yaacovCR
Copy link
Contributor

@yaacovCR yaacovCR commented Dec 9, 2022

UPDATE:

After much testing, I am unable to generalize the speed savings from #3754, so this PR has been closed.

See #3754 for further discussion.

prefer-await-over-promises helps avoid extra ticks, formalizing the optimization from #3754 across the board

various additional rules add additional best practices with regard to promise use with at most trivial code changes

@netlify
Copy link

netlify bot commented Dec 9, 2022

Deploy Preview for compassionate-pike-271cb3 ready!

Name Link
🔨 Latest commit 8cebd7e
🔍 Latest deploy log https://app.netlify.com/sites/compassionate-pike-271cb3/deploys/63aa9678fc785000088d4925
😎 Deploy Preview https://deploy-preview-3796--compassionate-pike-271cb3.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented Dec 9, 2022

Hi @yaacovCR, I'm @github-actions bot happy to help you with this PR 👋

Supported commands

Please post this commands in separate comments and only one per comment:

  • @github-actions run-benchmark - Run benchmark comparing base and merge commits for this PR
  • @github-actions publish-pr-on-npm - Build package from this PR and publish it on NPM

@yaacovCR yaacovCR requested a review from a team December 13, 2022 04:30
@yaacovCR yaacovCR added the PR: bug fix 🐞 requires increase of "patch" version number label Dec 13, 2022
@yaacovCR yaacovCR marked this pull request as ready for review December 13, 2022 04:31
@yaacovCR
Copy link
Contributor Author

This ends up being a "bug fix" because it does remove an extra tick and so is observable.

@yaacovCR yaacovCR changed the title initial steps to prefer-await-over-promises add eslint rules for promises Dec 13, 2022
@yaacovCR yaacovCR force-pushed the even-more branch 2 times, most recently from 660c90c to b057bc9 Compare December 13, 2022 04:50
package.json Outdated Show resolved Hide resolved
.eslintrc.yml Show resolved Hide resolved
@yaacovCR yaacovCR force-pushed the even-more branch 4 times, most recently from da0a5a4 to f1ba488 Compare December 16, 2022 04:08
@yaacovCR yaacovCR force-pushed the even-more branch 2 times, most recently from b66bdd3 to 1c39356 Compare December 16, 2022 11:30
@yaacovCR
Copy link
Contributor Author

#3801 extracted from this PR => with this PR now rebased on top of #3801

@yaacovCR
Copy link
Contributor Author

For simplicity, I have dropped the specialized helpers, provider just a single after helper than works pretty similar to .then.

...and enable basic rules
leads to one less tick in `executeStreamField` in the branch where completion of a non-promise yields a promise.
errors avoided by previously avoiding then/catch
we can't fix then, but we can replace it.

instead of using immediately invoked anonymous asynchronous arrow functions, we can replace the then method with then-like helper functions. In fact, we can optimize these small helpers to the exact cases we need.

= after() => helper with only onFulfilled callback
= catchAfter => helper with onError callback that handles promise rejection
= tryAfter => helper with synchronous onFulfilled callback and onError callback that handles promise rejection and onFulfilled exceptions
Motivation:  It is faster to await a promise prior to returning it from an async function than to return a promise with `.then()`.

See: https://github.com/tc39/proposal-faster-promise-adoption

Previously, this eslint rule errored by default only 'in-try-catch'.
`after` becomes sync, requiring no return type check for the onFulfilled result.
@yaacovCR
Copy link
Contributor Author

Closing this, see edited comment on #3754.

The real world implications of the extra tick within .then(...) as opposed to async functions is not clear. It does still seem prudent to consolidate .then(...) calls when possible, as extra .then(...) calls seem to have been the reason for the performance benefit of #3754.

@yaacovCR yaacovCR closed this Dec 27, 2022
yaacovCR added a commit to yaacovCR/graphql-js that referenced this pull request Dec 27, 2022
We could not demonstrate via actual benchmarking that the tick reduction from awaiting a promise prior to returning it translates into real-world performance gains. See graphql#3796.

In any case, the promise must be awaited (irrespective of performance) to allow the catch block to handle rejection errors. Simply returning `completed` would result in test failures.
@yaacovCR yaacovCR deleted the even-more branch December 27, 2022 13:36
yaacovCR added a commit that referenced this pull request Jan 2, 2023
We could not demonstrate via actual benchmarking that the tick reduction
from awaiting a promise prior to returning it translates into real-world
performance gains. See #3796.

In any case, the promise must be awaited (irrespective of performance)
to allow the catch block to handle rejection errors. Simply returning
`completed` would result in test failures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: bug fix 🐞 requires increase of "patch" version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants