no-floating-promises
Require Promise-like statements to be handled appropriately.
"floating" Promise 是在创建时没有设置任何代码来处理它可能引发的任何错误的 Promise。 浮动 Promise 可能会导致多种问题,例如操作顺序不正确、忽略 Promise 拒绝等等。
英:A "floating" Promise is one that is created without any code set up to handle any errors it might throw. Floating Promises can cause several issues, such as improperly sequenced operations, ignored Promise rejections, and more.
此规则报告何时创建 Promise 且未正确处理。 处理 Promise 值语句的有效方法包括:
英:This rule reports when a Promise is created and not properly handled. Valid ways of handling a Promise-valued statement include:
await
了它return
了它- 用两个参数调用它的
.then()
- 用一个参数调用它的
.catch()
此规则还会报告何时创建包含 Promise 的数组且未正确处理。 解决此问题的主要方法是使用 Promise 并发方法之一创建单个 Promise,然后根据上述过程进行处理。 这些方法包括:
英:This rule also reports when an Array containing Promises is created and not properly handled. The main way to resolve this is by using one of the Promise concurrency methods to create a single Promise, then handling that according to the procedure above. These methods include:
Promise.all()
,Promise.allSettled()
,Promise.any()
Promise.race()
no-floating-promises
仅检测未处理的 Promise 语句。
请参阅 no-misused-promises
来检测向逻辑位置(例如 if 语句)提供 Promise 的代码。
module.exports = {
"rules": {
"@typescript-eslint/no-floating-promises": "error"
}
};
示例
- ❌ 不正确
- ✅ 正确
const promise = new Promise((resolve, reject) => resolve('value'));
promise;
async function returnsPromise() {
return 'value';
}
returnsPromise().then(() => {});
Promise.reject('value').catch();
Promise.reject('value').finally();
[1, 2, 3].map(async x => x + 1);
Open in Playgroundconst promise = new Promise((resolve, reject) => resolve('value'));
await promise;
async function returnsPromise() {
return 'value';
}
returnsPromise().then(
() => {},
() => {},
);
Promise.reject('value').catch(() => {});
await Promise.reject('value').finally(() => {});
await Promise.all([1, 2, 3].map(async x => x + 1));
Open in Playground选项
ignoreVoid
这允许你停止使用 void 运算符消耗的规则报告 promise。 这可能是明确将 promise 标记为有意不等待的好方法。
英:This allows you to stop the rule reporting promises consumed with void operator. This can be a good way to explicitly mark a promise as intentionally not awaited.
此规则的 correct 代码与 { ignoreVoid: true }
的示例:
英:Examples of correct code for this rule with { ignoreVoid: true }
:
async function returnsPromise() {
return 'value';
}
void returnsPromise();
void Promise.reject('value');
Open in Playground将此选项设置为 true
,如果你使用 no-void
,则应打开 allowAsStatement
选项。
英:With this option set to true
, and if you are using no-void
, you should turn on the allowAsStatement
option.
ignoreIIFE
这允许你跳过异步 IIFE(立即调用函数表达式)的检查。
英:This allows you to skip checking of async IIFEs (Immediately Invoked function Expressions).
此规则的 correct 代码与 { ignoreIIFE: true }
的示例:
英:Examples of correct code for this rule with { ignoreIIFE: true }
:
await(async function () {
await res(1);
})();
(async function () {
await res(1);
})();
Open in Playground何时不使用它
在设置了许多浮动 Promise 的大型现有项目上,可能很难启用此规则。
或者,如果你不担心浮动或误用 Promise 导致的崩溃(例如,如果你注册了全局未处理的 Promise 处理程序),那么在某些情况下,不使用此规则可能是安全的。
你可以考虑在这些特定情况下使用 void
和/或 ESLint 禁用注释,而不是完全禁用此规则。
英:This rule can be difficult to enable on large existing projects that set up many floating Promises.
Alternately, if you're not worried about crashes from floating or misused Promises -such as if you have global unhandled Promise handlers registered- then in some cases it may be safe to not use this rule.
You might consider using void
s and/or ESLint disable comments for those specific situations instead of completely disabling this rule.
相关
进一步阅读
- "使用 Promise" MDN 文档。 特别注意 Promise 拒绝事件 和 组合 部分。
选项
该规则接受以下选项
type Options = [
{
/** Whether to ignore async IIFEs (Immediately Invoked Function Expressions). */
ignoreIIFE?: boolean;
/** Whether to ignore `void` expressions. */
ignoreVoid?: boolean;
},
];
const defaultOptions: Options = [{ ignoreVoid: true, ignoreIIFE: false }];