コンテンツにスキップ

noMisusedPromises

このコンテンツはまだ日本語訳がありません。

biome.json
{
"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.

promise-in-condition.js
const promise = Promise.resolve('value');
if (promise) { /* This branch will always execute */ }
/promise-in-condition.js:2:5 lint/nursery/noMisusedPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A Promise was found where a conditional was expected.

1 │ const promise = Promise.resolve(‘value’);
> 2 │ if (promise) { /* This branch will always execute */ }
^^^^^^^
3 │

A Promise is always truthy, so this is most likely a mistake.

You may have intended to await the Promise instead.

promise-in-ternary-condition.js
const promise = Promise.resolve('value');
const val = promise ? 123 : 456; // Always evaluates to `123`.
/promise-in-ternary-condition.js:2:13 lint/nursery/noMisusedPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A Promise was found where a conditional was expected.

1 │ const promise = Promise.resolve(‘value’);
> 2 │ const val = promise ? 123 : 456; // Always evaluates to 123.
^^^^^^^
3 │

A Promise is always truthy, so this is most likely a mistake.

You may have intended to await the Promise instead.

promise-in-filter.js
// The following filter has no effect:
const promise = Promise.resolve('value');
[1, 2, 3].filter(() => promise);
/promise-in-filter.js:3:18 lint/nursery/noMisusedPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This function returns a Promise where a conditional was expected.

1 │ // The following filter has no effect:
2 │ const promise = Promise.resolve(‘value’);
> 3 │ [1, 2, 3].filter(() => promise);
^^^^^^^^^^^^^
4 │

A Promise is always truthy, so this is most likely a mistake.

You may have intended to await the Promise, but this does not work inside a synchronous callback.

promise-while-condition.js
const promise = Promise.resolve('value');
while (promise) { /* This is an endless loop */ }
/promise-while-condition.js:2:8 lint/nursery/noMisusedPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A Promise was found where a conditional was expected.

1 │ const promise = Promise.resolve(‘value’);
> 2 │ while (promise) { /* This is an endless loop */ }
^^^^^^^
3 │

A Promise is always truthy, so this is most likely a mistake.

You may have intended to await the Promise instead.

spread-promise.js
// Using a `Promise` as an iterable expands to nothing:
const getData = () => fetch('/');
console.log({ foo: 42, ...getData() });
/spread-promise.js:3:27 lint/nursery/noMisusedPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

A Promise was found where an iterable was expected.

1 │ // Using a Promise as an iterable expands to nothing:
2 │ const getData = () => fetch(’/’);
> 3 │ console.log({ foo: 42, …getData() });
^^^^^^^^^
4 │

The spread syntax is used to expand an iterable, but a Promise needs to be await-ed to take its value.

promise-in-forEach.js
// These `fetch`-es are not `await`-ed in order:
[1, 2, 3].forEach(async value => {
await fetch(`/${value}`);
});
/promise-in-forEach.js:2:19 lint/nursery/noMisusedPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This function returns a Promise, but no return value was expected.

1 │ // These fetch-es are not await-ed in order:
> 2 │ [1, 2, 3].forEach(async value => {
^^^^^^^^^^^^^^^^
> 3 │ await fetch(/${value});
> 4 │ });
^
5 │

This may not have the desired result if you expect the Promise to be await-ed.

valid-promises.js
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);
}