Skip to content

noFloatingPromises

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:

  • Calling its .then() method with two arguments
  • Calling its .catch() method with one argument
  • awaiting it
  • returning it
  • voiding it
async 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();
}
biome.json
{
"linter": {
"rules": {
"nursery": {
"noFloatingPromises": "error"
}
}
}
}