Skip to content

noFloatingPromises

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"noFloatingPromises": "error"
}
}
}
}

¥Description

要求正确处理类似 Promise 的语句。

¥Require Promise-like statements to be handled appropriately.

“floating” Promise 是指在创建时未设置任何代码来处理可能抛出的错误。浮动 Promise 可能会导致多种问题,包括操作顺序错误、未处理的 Promise 拒绝以及其他意外后果。

¥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.

此规则会报告未按以下方式之一处理的 Promise 值语句:

¥This rule will report Promise-valued statements that are not treated in one of the following ways:

  • 使用两个参数调用其 .then() 方法

    ¥Calling its .then() method with two arguments

  • 使用一个参数调用其 .catch() 方法

    ¥Calling its .catch() method with one argument

  • 使用 await

    ¥await-ing it

  • 使用 return

    ¥return-ing it

  • 使用 void

    ¥void-ing it

¥Examples

¥Invalid

async-fn.ts
async function returnsPromise(): Promise<string> {
return 'value';
}
returnsPromise().then(() => {});
/async-fn.ts:4:1 lint/nursery/noFloatingPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.

2 │ return ‘value’;
3 │ }
> 4 │ returnsPromise().then(() => {});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 │

This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.

async-fn2.ts
const returnsPromise = async (): Promise<string> => {
return 'value';
}
async function returnsPromiseInAsyncFunction() {
returnsPromise().then(() => {});
}
/async-fn2.ts:5:3 lint/nursery/noFloatingPromises  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.

3 │ }
4 │ async function returnsPromiseInAsyncFunction() {
> 5 │ returnsPromise().then(() => {});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 │ }
7 │

This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.

Unsafe fix: Add await operator.

5 │ ··await·returnsPromise().then(()·=>·{});
++++++
new-promise.js
const promise = new Promise((resolve) => resolve('value'));
promise.then(() => { }).finally(() => { });
/new-promise.js:2:1 lint/nursery/noFloatingPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.

1 │ const promise = new Promise((resolve) => resolve(‘value’));
> 2 │ promise.then(() => { }).finally(() => { });
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │

This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.

promise-all.js
Promise.all([p1, p2, p3])
/promise-all.js:1:1 lint/nursery/noFloatingPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.

> 1 │ Promise.all([p1, p2, p3])
^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.

async-method.ts
class Api {
async returnsPromise(): Promise<string> {
return 'value';
}
async someMethod() {
this.returnsPromise();
}
}
/async-method.ts:6:5 lint/nursery/noFloatingPromises  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.

4 │ }
5 │ async someMethod() {
> 6 │ this.returnsPromise();
^^^^^^^^^^^^^^^^^^^^^^
7 │ }
8 │ }

This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.

Unsafe fix: Add await operator.

6 │ ····await·this.returnsPromise();
++++++
async-super-method.ts
class Parent {
async returnsPromise(): Promise<string> {
return 'value';
}
}
class Child extends Parent {
async someMethod() {
this.returnsPromise();
}
}
/async-super-method.ts:9:5 lint/nursery/noFloatingPromises  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.

7 │ class Child extends Parent {
8 │ async someMethod() {
> 9 │ this.returnsPromise();
^^^^^^^^^^^^^^^^^^^^^^
10 │ }
11 │ }

This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.

Unsafe fix: Add await operator.

9 │ ····await·this.returnsPromise();
++++++
async-method2.ts
class Api {
async returnsPromise(): Promise<string> {
return 'value';
}
}
const api = new Api();
api.returnsPromise().then(() => {}).finally(() => {});
/async-method2.ts:7:1 lint/nursery/noFloatingPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.

5 │ }
6 │ const api = new Api();
> 7 │ api.returnsPromise().then(() => {}).finally(() => {});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8 │

This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.

async-object-method.ts
const obj = {
async returnsPromise(): Promise<string> {
return 'value';
},
};
obj.returnsPromise();
/async-object-method.ts:7:1 lint/nursery/noFloatingPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.

5 │ };
6 │
> 7 │ obj.returnsPromise();
^^^^^^^^^^^^^^^^^^^^^
8 │

This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.

async-prop.ts
type Props = {
returnsPromise: () => Promise<void>;
};
async function testCallingReturnsPromise(props: Props) {
props.returnsPromise();
}
/async-prop.ts:6:3 lint/nursery/noFloatingPromises  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A “floating” Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.

5 │ async function testCallingReturnsPromise(props: Props) {
> 6 │ props.returnsPromise();
^^^^^^^^^^^^^^^^^^^^^^^
7 │ }
8 │

This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.

Unsafe fix: Add await operator.

6 │ ··await·props.returnsPromise();
++++++

¥Valid

valid-examples.ts
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();
}

¥Related links