src/content/docs/linter/rules/no-misused-promises.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="TypeScript and TSX" icon="seti:typescript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: :::note This rule belongs to the types domain. This means that its activation will activate the Biome Scanner to scan the files of your project, and enable the type inference engine. Read more about it in the [documentation page](/linter/domains#types) ::: ## Summary - Rule available since: `v2.1.0` - Diagnostic Category: [`lint/nursery/noMisusedPromises`](/reference/diagnostics#diagnostic-category) - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - This rule belongs to the following domains: - [`types`](/linter/domains#types) - Sources: - Same as [`@typescript-eslint/no-misused-promises`](https://typescript-eslint.io/rules/no-misused-promises){
"linter": {
"rules": {
"nursery": {
"noMisusedPromises": "error"
}
}
}
}
Disallow Promises to be used in places where they are almost certainly a mistake.
In most cases, if you assign a Promise somewhere a Promise is not
allowed, the TypeScript compiler will be able to catch such a mistake.
But there are a few places where TypeScript allows them -- they're not
necessarily a mistake -- even though they could be considered almost
certainly to be one.
This rule disallows using Promises in such places.
const promise = Promise.resolve('value');
if (promise) { /* This branch will always execute */ }
const promise = Promise.resolve('value');
const val = promise ? 123 : 456; // Always evaluates to `123`.
// The following filter has no effect:
const promise = Promise.resolve('value');
[1, 2, 3].filter(() => promise);
const promise = Promise.resolve('value');
while (promise) { /* This is an endless loop */ }
// Using a `Promise` as an iterable expands to nothing:
const getData = () => fetch('/');
console.log({ foo: 42, ...getData() });
// These `fetch`-es are not `await`-ed in order:
[1, 2, 3].forEach(async value => {
await fetch(`/${value}`);
});
const promise = Promise.resolve('value');
if (await promise) { /* Do something */ }
const val = (await promise) ? 123 : 456;
while (await promise) { /* Do something */ }
const getData = () => fetch('/');
console.log({ foo: 42, ...(await getData()) });
// for-of puts `await` in outer context:
for (const value of [1, 2, 3]) {
await doSomething(value);
}