src/content/docs/linter/rules/no-floating-promises.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="TypeScript and TSX" icon="seti:typescript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: :::note This rule belongs to the types domain. This means that its activation will activate the Biome Scanner to scan the files of your project, and enable the type inference engine. Read more about it in the [documentation page](/linter/domains#types) ::: ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/nursery/noFloatingPromises`](/reference/diagnostics#diagnostic-category) - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - This rule belongs to the following domains: - [`types`](/linter/domains#types) - Sources: - Same as [`@typescript-eslint/no-floating-promises`](https://typescript-eslint.io/rules/no-floating-promises){
"linter": {
"rules": {
"nursery": {
"noFloatingPromises": "error"
}
}
}
}
Require Promise-like statements to be handled appropriately.
A "floating" Promise is one that is created without any code set up to
handle any errors it might throw. Floating Promises can lead to several
issues, including improperly sequenced operations, unhandled Promise
rejections, and other unintended consequences.
This rule will report Promise-valued statements that are not treated in one of the following ways:
.then() method with two arguments.catch() method with one argumentawait-ing itreturn-ing itvoid-ing itasync function returnsPromise(): Promise<string> {
return 'value';
}
returnsPromise().then(() => {});
const returnsPromise = async (): Promise<string> => {
return 'value';
}
async function returnsPromiseInAsyncFunction() {
returnsPromise().then(() => {});
}
const promise = new Promise((resolve) => resolve('value'));
promise.then(() => { }).finally(() => { });
Promise.all([p1, p2, p3])
class Api {
async returnsPromise(): Promise<string> {
return 'value';
}
async someMethod() {
this.returnsPromise();
}
}
class Parent {
async returnsPromise(): Promise<string> {
return 'value';
}
}
class Child extends Parent {
async someMethod() {
this.returnsPromise();
}
}
class Api {
async returnsPromise(): Promise<string> {
return 'value';
}
}
const api = new Api();
api.returnsPromise().then(() => {}).finally(() => {});
const obj = {
async returnsPromise(): Promise<string> {
return 'value';
},
};
obj.returnsPromise();
type Props = {
returnsPromise: () => Promise<void>;
};
async function testCallingReturnsPromise(props: Props) {
props.returnsPromise();
}
async function returnsPromise(): Promise<string> {
return 'value';
}
await returnsPromise();
void returnsPromise();
// Calling .then() with two arguments
returnsPromise().then(
() => {},
() => {},
);
// Calling .catch() with one argument
returnsPromise().catch(() => {});
await Promise.all([p1, p2, p3])
class Api {
async returnsPromise(): Promise<string> {
return 'value';
}
async someMethod() {
await this.returnsPromise();
}
}
type Props = {
returnsPromise: () => Promise<void>;
};
async function testCallingReturnsPromise(props: Props) {
return props.returnsPromise();
}