noAsyncPromiseExecutor
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/suspicious/noAsyncPromiseExecutor¥Diagnostic Category:
lint/suspicious/noAsyncPromiseExecutor -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
no-async-promise-executor相同¥Same as
no-async-promise-executor
-
¥How to configure
{ "linter": { "rules": { "suspicious": { "noAsyncPromiseExecutor": "error" } } }}¥Description
禁止使用异步函数作为 Promise 执行器。
¥Disallows using an async function as a Promise executor.
执行器函数也可以是异步函数。但是,这通常是一个错误,原因如下:
¥The executor function can also be an async function. However, this is usually a mistake, for a few reasons:
-
如果异步执行器函数抛出错误,错误将丢失,不会导致新构造的
Promise拒绝。这可能会使调试和处理某些错误变得困难。¥If an async executor function throws an error, the error will be lost and won’t cause the newly-constructed
Promiseto reject. This could make it difficult to debug and handle some errors. -
如果 Promise 执行器函数正在使用
await,这通常表明实际上没有必要使用new Promise构造函数,或者可以缩小new Promise构造函数的范围。¥If a Promise executor function is using
await, this is usually a sign that it is not actually necessary to use thenew Promiseconstructor, or the scope of thenew Promiseconstructor can be reduced.
¥Examples
¥Invalid
new Promise(async function foo(resolve, reject) {})code-block.js:1:13 lint/suspicious/noAsyncPromiseExecutor ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Promise executor functions should not be `async`.
> 1 │ new Promise(async function foo(resolve, reject) {})
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
new Promise(async (resolve, reject) => {})code-block.js:1:15 lint/suspicious/noAsyncPromiseExecutor ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Promise executor functions should not be `async`.
> 1 │ new Promise(async (resolve, reject) => {})
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
new Promise(((((async () => {})))))code-block.js:1:19 lint/suspicious/noAsyncPromiseExecutor ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Promise executor functions should not be `async`.
> 1 │ new Promise(((((async () => {})))))
│ ^^^^^^^^^^^^^^
2 │
¥Valid
new Promise((resolve, reject) => {}) new Promise((resolve, reject) => {}, async function unrelated() {}) new Foo(async (resolve, reject) => {}) new Foo((( (resolve, reject) => {} )))¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号