noMisusedPromises
¥Summary
-
规则生效日期:
v2.1.0¥Rule available since:
v2.1.0 -
诊断类别:
lint/nursery/noMisusedPromises¥Diagnostic Category:
lint/nursery/noMisusedPromises -
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
此规则属于以下域:
¥This rule belongs to the following domains:
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "nursery": { "noMisusedPromises": "error" } } }}¥Description
禁止在几乎可以肯定是错误的地方使用 Promise。
¥Disallow Promises to be used in places where they are almost certainly a mistake.
在大多数情况下,如果你在不允许使用 Promise 的地方使用了 Promise,TypeScript 编译器能够捕获到此类错误。但 TypeScript 在某些地方允许出现一些特殊情况 - 这些情况未必是错误 - 即使它们几乎可以肯定是错误。
¥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.
此规则禁止在此类地方使用 Promise。
¥This rule disallows using Promises in such places.
¥Examples
¥Invalid
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.
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.
// 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.
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.
// 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.
// 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
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);}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号