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

New Rule: Prefer Promise.resolve(foo) to new Promise((resolve) => resolve(foo)) #448

Open
NotWoods opened this issue Jan 4, 2024 · 1 comment · May be fixed by #473
Open

New Rule: Prefer Promise.resolve(foo) to new Promise((resolve) => resolve(foo)) #448

NotWoods opened this issue Jan 4, 2024 · 1 comment · May be fixed by #473

Comments

@NotWoods
Copy link

NotWoods commented Jan 4, 2024

Description

Rule to recommend using Promise static resolve/reject methods over new Promise, which saves a microtick and is more readable.

Using new Promise for simple use cases is a common mistake when developers first start working with promises. This rule would check for simple use cases, where resolve or reject is immediately called in the promise executor. If the executor function is more complex then the rule isn't triggered.

Possible name: prefer-promise-static-methods

Steps to Reproduce

Lint the given code and expect to see errors

new Promise((resolve) => resolve(foo));
new Promise((resolve) => { resolve(foo) });
// autofix to Promise.resolve(foo);

new Promise((_, reject) => reject(foo));
new Promise((_, reject) => { reject(foo) });
// autofix to Promise.reject(foo);

Versions

N/A

Additional Information

Willing to submit a PR, I've already written this rule internally for Microsoft and would like to upstream it

@voxpelli
Copy link
Member

I like the idea and a PR would be much appreciated 🙏

@NotWoods NotWoods linked a pull request May 29, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants