@typescript-eslint/no-floating-promises
Require promises to be awaited, returned, or handled
Also called typescript-eslint/no-floating-promises, typescript/no-floating-promises
A promise that nobody awaits runs on its own. If it rejects, the try around the call doesn't catch the error, and the code after it runs before the work is done.
A promise counts as handled if it's awaited, returned, given a .catch() or a second argument to .then(), or marked as ignored on purpose with void.
bun lint --type-aware turns this rule on in TypeScript files.
Configure#
Off by default. This rule needs types, so turning it on makes bun lint type check your code first.
[lint.rules]
"@typescript-eslint/no-floating-promises" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"@typescript-eslint/no-floating-promises" = ["error", {
ignoreVoid = true,
ignoreIIFE = false,
checkThenables = false,
allowForKnownSafePromises = [], # list of strings or objects
allowForKnownSafeCalls = [], # list of strings or objects
}]Examples#
async function save(data: string): Promise<void> {}
function onClick() {
save("draft");Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the void operator.
}async function save(data: string): Promise<void> {}
async function onClick() {
await save("draft");
}
function onBlur() {
void save("draft");
}Options#
| Option | Default | What it does |
|---|---|---|
ignoreVoid | true | Allow void promise as a way to say "ignored on purpose". |
ignoreIIFE | false | Allow an async function that's called where it's written: (async () => { ... })(). |
checkThenables | false | Also report objects that only have a then method. |
allowForKnownSafePromises | [] | Promise types that never need handling. |
allowForKnownSafeCalls | [] | Functions that return promises you never need to handle. |
With ignoreVoid set to false, this is reported:
{ "ignoreVoid": false }1 problemasync function save(data: string): Promise<void> {}
void save("draft");Promises must be awaited, end with a call to .catch, or end with a call to .then with a rejection handler.An entry in allowForKnownSafePromises or allowForKnownSafeCalls is a name, or an object that also says where the name is declared:
| Entry | What it matches |
|---|---|
"SafePromise" | Anything with that name. |
{ "from": "file", "name": "SafePromise" } | A name declared in your project. |
{ "from": "lib", "name": "PromiseLike" } | A name from the built-in TypeScript types. |
{ "from": "package", "name": "track", "package": "analytics" } | A name from a package. |
With { "from": "file", "name": "SafePromise" } in allowForKnownSafePromises, this isn't reported:
{ "allowForKnownSafePromises": [{ "from": "file", "name": "SafePromise" }] }No problemstype SafePromise = Promise<void> & { neverRejects: true };
declare function log(message: string): SafePromise;
log("saved");With { "from": "file", "name": "schedule" } in allowForKnownSafeCalls, neither is this:
{ "allowForKnownSafeCalls": [{ "from": "file", "name": "schedule" }] }No problemsdeclare function schedule(job: () => void): Promise<void>;
schedule(() => console.log("tick"));