💭 [lint] propose new rules around the correct usage of Promise
#1094
Replies: 18 comments
-
There are promises that never reject. I don't think that
What's the argument against the example you posted? Isn't that just a normal |
Beta Was this translation helpful? Give feedback.
-
We can limit the rule to cases that we know require
Having promiseAction().then((value) => {
processValue(value);
// here the chain is broken because we didn't return
}).then(() => {
doSomethingElse();
}); In this example, With the advent of sideEffect()
.then(foo)
.then(bar)
.then(lorem) In order achieve that, we have to make sure that we always return something. |
Beta Was this translation helpful? Give feedback.
-
I think we should force the Promise.allSettled([sidEffect1, sideEffect2]).then([result1, result2] => {
if (result2) {
throw new Error();
// alternatively, we call a function that throws an error by mistake
}
}); In this example the error gets swallowed because there's no |
Beta Was this translation helpful? Give feedback.
-
I don't think that this is true. The
It may be possible to implement a narrow version of this rule that detects the specific mistake in that example, but I think it would feel very strange to enforce code like this: promiseThatNeverRejects.then((data) => {
functionThatNeverThrows(data);
}).catch(() => {
// unreachable code that Rome insists on
}); |
Beta Was this translation helpful? Give feedback.
-
Well it's partially true. Check this simple sandbox I made. Without return, the last log is printed first. With the return, the chain of promises is correctly executed. https://codesandbox.io/s/mystifying-mclaren-14n8g?file=/src/index.js I am not saying that what you're saying is false, actually my example was bad. So sorry. I think to enforce the
A promise without a |
Beta Was this translation helpful? Give feedback.
-
Even Promise.allSettled can reject, if the receiver’s constructor lacks the resolve method. |
Beta Was this translation helpful? Give feedback.
-
It's just showing some placeholder code for me. Can you check if it wasn't saved or if the problem is on my end?
While I still don't think that this is always true, I agree that it's usually true, so it does seem plausible that the overall code quality improvements from enforcing |
Beta Was this translation helpful? Give feedback.
-
Ah, I didn't know that. Thanks for the info. |
Beta Was this translation helpful? Give feedback.
-
@yassere Could you try again with this URL please? https://codesandbox.io/s/gifted-goodall-qdtgh?file=/src/index.js |
Beta Was this translation helpful? Give feedback.
-
@ematipico Ah, I understand what you're saying now. If you want your promise chain to wait on a promise, then you need to return that promise from your handler. That's true, but that's not always necessary and I think that the following code is normal: getData().then((data) => {
const result = processSynchronously(data);
dispatch(result);
}).catch((err) => {
handleError(err);
}); I'm not trying to be negative. I do think these could be useful rules that would catch errors. But I also want to be cautious about rules that produce diagnostics on technically valid code and don't have reliable autofixes. I think we have a limited "budget" for how much we can expect people to manually change valid code to appease Rome, and I just want to be careful with it. |
Beta Was this translation helpful? Give feedback.
-
Yeah this makes sense. What if the rules checks for the usage of at least two Also wonder if we'd be able to check this: function a() {
console.log()
}
promise.then(a).then(somethingElse); In this small example the function |
Beta Was this translation helpful? Give feedback.
-
That's incorrect, though. There's zero reason whatsoever that |
Beta Was this translation helpful? Give feedback.
-
Yes, but how many people know that? That's why I was wondering if we can have some linting around promises. I mean, if I know that Instead, if |
Beta Was this translation helpful? Give feedback.
-
That sounds like something a type system would handle, not a linter. There’s nothing about promises that suddenly enables static analysis of JS to know when a function is being called improperly. |
Beta Was this translation helpful? Give feedback.
-
It does get executed after
const aPromise = promise.then(a);
aPromise.then(somethingElse);
If If If |
Beta Was this translation helpful? Give feedback.
-
Thank you for the feedback! Nobody said anything around Regarding the other two suggestions, let's forget about the Is having a |
Beta Was this translation helpful? Give feedback.
-
A generic lint rule that could be useful is warning on |
Beta Was this translation helpful? Give feedback.
-
That sounds reasonable to me, although I agree with @ljharb that a generic rule could be useful. We have lists of builtins in
Here are a few more things to consider if we want to implement a
I think these would all work: const promise = getData().then(doSomething);
promise.catch(handleErr); function foo(str) {
return getData(str).then(doSomething);
}
foo("api").catch(handleErr); function addCatch(promise) {
promise.catch(handleErr);
}
addCatch(somePromise.then(doSomething)); We can't determine if the Also, this comment is relevant. |
Beta Was this translation helpful? Give feedback.
-
I'd like to propose some rule to enforce some best practice around the usage of
Promise
..catch
if using.then
:new
when using static methods:return
inside the.then
:Beta Was this translation helpful? Give feedback.
All reactions