-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Enhancement: [no-floating-promises] Don't check coincidentally Promise-like (Thenable) type values by default #8433
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
Comments
@Josh-Cena moving our comments from #9263 back over here for issue discussion:
Ah, I think I see what you're asking for - any reason why we would want to not check a thenable at all? That's a good question. I'd come into this assuming it was a generally-desirable feature, but now I'm questioning that. Maybe now that we have the Putting this issue into Thanks for pressing @Josh-Cena! |
The reason for not checking If an object isn't a promise or promise-like, but it just has a |
I'm not sure I understand the comment as the two paragraph say opposite things.
Put another way, either you have something that quacks like a promise and it's a bug to not await it, or it's not supposed to be a promise and it's a bug that The only valid use case I can think of is if you treat |
We use it in Fastify quite extensively. In fact, we cannot write correct types for Fastify because of typescript-eslint. Your users would start plastering all their code of unneeded We have to resort to tricks like this: instead of typing a .then() method into FastifyInstance. |
@mcollina what would be useful for us here and in fastify/fastify#5506 (comment) would be: why does the For example, is it intentional that someone would (to be clear: not imparting judgement on the code, just trying to understand the context) |
Fastify has a rich plugin system, and all plugins load asynchronously. We support an API like: const app = fastify()
await app
.register(myPlugin1)
.register(myPlugin2)
.register(myPlugin3) This is also supported: const app = fastify()
app.register(myPlugin1)
app.register(myPlugin2)
await app.register(myPlugin3) Adding the Note that awaiting the instance is entirely optional: const app = fastify()
app.register(myPlugin1)
app.register(myPlugin2)
app.register(myPlugin3)
await app.listen({ port: 3000 }) There are also other methods on the app: const app = fastify()
app.get('/', ...).post('/', ...)
await app.listen({ port: 3000 }) The const app = fastify()
await app.get('/', ...) // or void app.get('/', ...)
await app.post('/', ...)
await app.listen({ port: 3000 }) This would work, but it adds unnecessary overhead and worsens the framework's DX. Note that our const app = fastify()
app.get('/simple', async function (req, reply) {
return { hello: 'world' }
})
app.get('/complex', async function (req, reply) {
// calling `.then()` here is incorrect, as it will wait for the response to be sent
// however no-floating-promises forces the user to add a `void`
void reply.header('x-foo', 'bar')
setImmediate(() => {
// Note that this break the promise chain
reply.send('something')
})
return reply // this will can reply.then()
})
app.get('/empty', async function (req, reply) {
// this will return an empty response
}) |
I see. So you have a chaining API where each chained operation can be optionally awaited, but usually they are not necessary. Yes it would be tricky to design such checks. Won't the Given that the rule is called |
This would need to be enabled by the end users, and I would rather prefer if this wasn't needed at all. None of these are promises after all, and adding custom configs in every project is annoying. |
Sure, I see the reasoning there. I think only checking native promises would be the least surprising behavior. |
Also noting @mcollina's comment from fastify/fastify#5506 (comment) regarding
|
Agreed, which is what I see this issue as trying to approach. I'm also wondering if instead of an "all or nothing" boolean option value, we should turn
...where maybe the We're going to have to think on this a bit. Context: as with
That's what |
Note that |
Yeah, I don't think |
Ok so coming back to this: what is the consensus here? I'm:
|
Also confirmed with @Josh-Cena 1:1 that we're good to continue. Great! 🚀 |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Link to the rule's documentation
https://typescript-eslint.io/rules/no-floating-promises
Description
Forwarding out from conversations with @mcollina, especially nodejs/node#51292 (comment): right now
no-floating-promises
will consider anything that looks like a Promise (i.e. is "Promise-like") to be a potential floating value:typescript-eslint/packages/eslint-plugin/src/rules/no-floating-promises.ts
Line 353 in 5857356
But to my knowledge this hasn't been particularly helpful. If something coincidentally happens to match the
Thenable
interface that doesn't necessarily mean it shouldn't float!The historical context here is that it used to be common in JavaScript code to use alternative implementations of Promises, such as Bluebird. Promises were only added to some browsers in ~2014 and Node & co in ~2015 or so. Many sites still had to support old versions of Internet Explorer. The
no-floating-promises
typescript-eslint rule was implemented in 2019 when it was still not-unheard-of to use alternative Promises or at least have Promise types provided by a polyfill: #495.Now that the vast majority of projects are using built-in Promise values & types, I think the value of detecting those values automatically is no longer worth the pain of flagging general thenables. I propose we modify the rule to:
Promise
typePromise
type from the list of checked types? I don't think so, but that's not a decision we'd need to make now...I.e. this is the opposite of #7008. That issue is about adding an allowlist for types to be allowed to float. This one is adding a blocklist of types to not be allowed to float - instead of always detecting Thenables.
Fail
Pass
Additional Info
After the proposed followup, if folks still want to detect floating
PromiseLike
s, they'd be able to use this option.As to whether we'd want to give users a way to opt back into the current "check anything that looks like a Thenable" behavior... I don't see why anybody would want that, but we could always make it an option if someone asks for it.
The text was updated successfully, but these errors were encountered: