noFloatingPromises
Summary
Section titled Summary- Diagnostic Category:
lint/nursery/noFloatingPromises
- This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
Description
Section titled DescriptionRequire 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
Examples
Section titled ExamplesInvalid
Section titled Invalidasync function returnsPromise(): Promise<string> { return 'value';}returnsPromise().then(() => {});
code-block.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(() => {});}
code-block.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(() => { });
code-block.ts: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])
code-block.ts: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(); }}
code-block.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(); }}
code-block.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(() => {});
code-block.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();
code-block.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();}
code-block.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
Section titled Validasync 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" } } }}