noFloatingPromises
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noFloatingPromises
- This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
Description
Section titled “Description”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 await
ing itreturn
ing itvoid
ing it
Important notes
Section titled “Important notes”Examples
Section titled “Examples”Invalid
Section titled “Invalid”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 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();}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noFloatingPromises": "error" } } }}