noFloatingPromises
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/nursery/noFloatingPromises¥Diagnostic Category:
lint/nursery/noFloatingPromises -
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
此规则属于以下域:
¥This rule belongs to the following domains:
-
来源:
¥Sources:
¥How to configure
{ "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 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.
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(()·=>·{});
│ ++++++
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([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.
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();
│ ++++++
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();
│ ++++++
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.
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.
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
async function returnsPromise(): Promise<string> { return 'value';}
await returnsPromise();
void returnsPromise();
// Calling .then() with two argumentsreturnsPromise().then( () => {}, () => {},);
// Calling .catch() with one argumentreturnsPromise().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
Biome v2.1 中文网 - 粤ICP备13048890号