Back to Biomejs

useAwaitThenable

src/content/docs/linter/rules/use-await-thenable.mdx

latest7.2 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::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.3.9` - Diagnostic Category: [`lint/nursery/useAwaitThenable`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - This rule belongs to the following domains: - [`types`](/linter/domains#types) - Sources: - Inspired from [`@typescript-eslint/use-await-thenable`](https://typescript-eslint.io/rules/use-await-thenable)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"useAwaitThenable": "error"
			}
		}
	}
}

Description

Enforce that await is only used on Promise values.

:::caution At the moment, this rule only checks for instances of the global Promise class. This is a major shortcoming compared to the ESLint rule if you are using custom Promise-like implementations such as Bluebird or in-house solutions. :::

Examples

Invalid

js
await 'value';
<pre class="language-text"><code class="language-text"><a href="file:///invalid-primitive.js">/invalid-primitive.js</a>:1:1 <a href="https://biomejs.dev/linter/rules/use-await-thenable">lint/nursery/useAwaitThenable</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">&#96;await&#96; used on a non-Promise value.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>await 'value'; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This may happen if you accidentally used &#96;await&#96; on a synchronous value.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Please ensure the value is not a custom &quot;thenable&quot; implementation before removing the &#96;await&#96;: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global&#95;Objects/Promise#thenables</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
js
const createValue = () => 'value';
await createValue();
<pre class="language-text"><code class="language-text"><a href="file:///invalid-function-call.js">/invalid-function-call.js</a>:2:1 <a href="https://biomejs.dev/linter/rules/use-await-thenable">lint/nursery/useAwaitThenable</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">&#96;await&#96; used on a non-Promise value.</span> <strong>1 │ </strong>const createValue = () =&gt; 'value'; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong>await createValue(); <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This may happen if you accidentally used &#96;await&#96; on a synchronous value.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Please ensure the value is not a custom &quot;thenable&quot; implementation before removing the &#96;await&#96;: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global&#95;Objects/Promise#thenables</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

Valid

js
await Promise.resolve('value');

const createValue = async () => 'value';
await createValue();
</TabItem> </Tabs>