eslint-plugin-promise icon indicating copy to clipboard operation
eslint-plugin-promise copied to clipboard

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

Open NotWoods opened this issue 1 year ago • 1 comments
trafficstars

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

NotWoods avatar Jan 04 '24 23:01 NotWoods

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

voxpelli avatar May 27 '24 12:05 voxpelli