typescript/no-floating-promises Correctness
Qué hace
Esta regla prohíbe "floating" Promises in TypeScript code, which is a Promise that is created without any code to handle its resolution or rejection.
This rule will report Promise-valued statements that are not treated in one of the following ways:
- Calling its
.then()with two arguments - Calling its
.catch()with one argument awaiting itreturning itvoiding it
This rule also reports when an Array containing Promises is created and not properly handled. The main way to resolve this is by using one of the Promise concurrency methods to create a single Promise, then handling that according to the procedure above. These methods include:
Promise.all()Promise.allSettled()Promise.any()Promise.race()
¿Por qué es problemático?
Floating Promises can cause several issues, such as improperly sequenced operations, ignored Promise rejections, and more.
Ejemplos
Ejemplos de código incorrecto para esta regla:
const promise = new Promise((resolve, reject) => resolve("value"));
promise;
async function returnsPromise() {
return "value";
}
returnsPromise().then(() => {});
Promise.reject("value").catch();
Promise.reject("value").finally();
[1, 2, 3].map(async (x) => x + 1);Ejemplos de código correcto para esta regla:
const promise = new Promise((resolve, reject) => resolve("value"));
await promise;
async function returnsPromise() {
return "value";
}
void returnsPromise();
returnsPromise().then(
() => {},
() => {},
);
Promise.reject("value").catch(() => {});
await Promise.reject("value").finally(() => {});
await Promise.all([1, 2, 3].map(async (x) => x + 1));Configuración
Esta regla acepta un objeto de configuración con las siguientes propiedades:
allowForKnownSafeCalls
tipo: array
predeterminado: []
Allows specific calls to be ignored, specified as type or value specifiers.
allowForKnownSafeCalls[n]
tipo: object | string
Type or value specifier for matching specific declarations
Supports four types of specifiers:
- String specifier (deprecated): Universal match by name
"Promise"- File specifier: Match types/values declared in local files
{ "from": "file", "name": "MyType" }
{ "from": "file", "name": ["Type1", "Type2"] }
{ "from": "file", "name": "MyType", "path": "./types.ts" }- Lib specifier: Match TypeScript built-in lib types
{ "from": "lib", "name": "Promise" }
{ "from": "lib", "name": ["Promise", "PromiseLike"] }- Package specifier: Match types/values from npm packages
{ "from": "package", "name": "Observable", "package": "rxjs" }
{ "from": "package", "name": ["Observable", "Subject"], "package": "rxjs" }allowForKnownSafeCalls[n].from
tipo: "file"
allowForKnownSafeCalls[n].name
tipo: array | string
Name specifier that can be a single string or array of strings
allowForKnownSafeCalls[n].name[n]
tipo: string
allowForKnownSafeCalls[n].path
tipo: string
Optional file path to specify where the types or values must be declared. If omitted, all files will be matched.
allowForKnownSafePromises
tipo: array
predeterminado: []
Allows specific Promise types to be ignored, specified as type or value specifiers.
allowForKnownSafePromises[n]
tipo: object | string
Type or value specifier for matching specific declarations
Supports four types of specifiers:
- String specifier (deprecated): Universal match by name
"Promise"- File specifier: Match types/values declared in local files
{ "from": "file", "name": "MyType" }
{ "from": "file", "name": ["Type1", "Type2"] }
{ "from": "file", "name": "MyType", "path": "./types.ts" }- Lib specifier: Match TypeScript built-in lib types
{ "from": "lib", "name": "Promise" }
{ "from": "lib", "name": ["Promise", "PromiseLike"] }- Package specifier: Match types/values from npm packages
{ "from": "package", "name": "Observable", "package": "rxjs" }
{ "from": "package", "name": ["Observable", "Subject"], "package": "rxjs" }allowForKnownSafePromises[n].from
tipo: "file"
allowForKnownSafePromises[n].name
tipo: array | string
Name specifier that can be a single string or array of strings
allowForKnownSafePromises[n].name[n]
tipo: string
allowForKnownSafePromises[n].path
tipo: string
Optional file path to specify where the types or values must be declared. If omitted, all files will be matched.
checkThenables
tipo: boolean
predeterminado: false
Check for thenable objects that are not necessarily Promises.
ignoreIIFE
tipo: boolean
predeterminado: false
Ignore immediately invoked function expressions (IIFEs).
ignoreVoid
tipo: boolean
predeterminado: true
Ignore Promises that are void expressions.
Cómo usarla
To enable this rule using the config file or in the CLI, you can use:
{
"options": {
"typeAware": true
},
"rules": {
"typescript/no-floating-promises": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
options: { typeAware: true },
rules: {
"typescript/no-floating-promises": "error",
},
});oxlint --type-aware --deny typescript/no-floating-promisesVersión
Esta regla se añadió en v1.11.0.