noMisusedPromises
Ce contenu n’est pas encore disponible dans votre langue.
Summary
Section titled “Summary”- Rule available since:
v2.1.0
- Diagnostic Category:
lint/nursery/noMisusedPromises
- This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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);}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noMisusedPromises": "error" } } }}