Skip to content

Docs: [no-floating-promises] voiding Promise doesn't make it handled #9947

Closed
@alexandercerutti

Description

@alexandercerutti

Before You File a Documentation Request Please Confirm You Have Done The Following...

Suggested Changes

Hi!

[no-floating-promises] cite what follows:

[...]

This rule reports when a Promise is created and not properly handled. Valid ways of handling a Promise-valued statement include:

[...]
- voiding it
[...]

However, voiding a Promise doesn't actually make it handled. The result is just ignored.

If a voided Promise is rejected, an error is still thrown and thus the failure is just ignored. However, the failure is still there. In fact:

  • a listener for unhandledrejection still gets invoked;
  • an error is still logged in the console and eventually in enterprise logging systems (e.g. Sentry);

Therefore, it is not exactly correct to say that void operator is a valid way of handling a Promise. A promise should still be followed by a .catch() (even with a noop function) right after the invokation (not after or in an if).

This code proves what happens. I trusted this package, I voided some Promises I didn't expect could reject and, after enabling Sentry, I ended up with a ton of reports about unhandled rejections.

	window.addEventListener("unhandledrejection", (err) => {
		console.log("Unhandled Rejection", err);
	});

	async function f1() {
		return Promise.reject("Timeout expired");
	}

	void f1();
	console.log("test");

Logs orders:

  • (log) "test"
  • (log) "Unhandled Rejection"
  • (error) Uncaught (in promise) Timeout expired
immagine

I'd improve the documentation by putting a warning on void about this behavior, both above in the list (add a link to ignoreVoid) and under the ignoreVoid.

Runtime error messages might get improved as well, but as I'm on an older version of the extension (v6 if I see correctly), I still went to see the documentation to check if the usage is correct.

Code could be improved by requiring the .catch(() => { ... }) even for voided Promises or by setting { ignoreVoid: false } by default, as the behavior is potentially annoying.

Thank you

Affected URL(s)

https://typescript-eslint.io/rules/no-floating-promises/

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuedocumentationDocumentation ("docs") that needs adding/updatinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions