-
-
Notifications
You must be signed in to change notification settings - Fork 353
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 no-useless-promise-resolve-reject
rule
#1623
Add no-useless-promise-resolve-reject
rule
#1623
Conversation
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check all comments.
Another edge case, if the return statement is in (async function() {
try {
return Promise.reject(1)
} catch {
return 2
}
})() // Reject with 1 (async function() {
try {
throw 1
} catch {
return 2
}
})() // Resolve with 2 |
In that case should we report an error or not? The use of |
Report but no fix? I'm not sure, either. We can just skip fix if these calls in |
`yield` expressions outside generator functions are a syntax error.
@cherryblossom000 I did a refactor, I this the fix logic is more clear, hope it's fine. |
@fisker Looks good, thanks for the refactor. |
One problem to fix async () => (( Promise.reject(error) )); |
Sorry. one more async function * foo() {
(( yield Promise.reject(error) ));
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you!
@cherryblossom000 Thanks for contributing :) |
What about cases where we may want to reject the async with a literal, but having |
@diegocr I think you should only reject a promise with an
A rejected promise should indicate an error. Rejecting a promise is the async equivalent of If you’re doing something like |
Hi @cherryblossom000, i actually meant a numeric literal, so that later we can do quick strict comparisons. async foo(offset) {
if (offset < 1) return Promise.reject(ERANGE);
....
}
...
foo().catch((err) => {
if (err === ERANGE) bar();
....
}); Perhaps the rule could be improved through options to whitelist certain uses, meanwhile i've enabled it as a Warning only, but would like it as an Error to enforce on my team mates getting rid of Promise.resolve() there. |
|
class CustomError extends Error {
ERANGE = true
}
throw new CustomError()
foo().catch((err) => {
if (err?.ERANGE) bar();
....
})
|
Nice idea @fisker, except |
If I were you I would disable the |
Yeah, that might be worth considering as well. Alright, thanks guys for your time and help in any case :) |
Fixes #1609