noAsyncPromiseExecutor (since v0.7.0)
This rule is recommended by Rome.
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:
- 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. - 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) {}) error[js/noAsyncPromiseExecutor]: Promise executor functions should not be `async`.
┌─ js/noAsyncPromiseExecutor.js:1:13
│
1 │ new Promise(async function foo(resolve, reject) {})
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
new Promise(async (resolve, reject) => {}) error[js/noAsyncPromiseExecutor]: Promise executor functions should not be `async`.
┌─ js/noAsyncPromiseExecutor.js:1:15
│
1 │ new Promise(async (resolve, reject) => {})
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
new Promise(((((async () => {}))))) error[js/noAsyncPromiseExecutor]: Promise executor functions should not be `async`.
┌─ js/noAsyncPromiseExecutor.js:1:19
│
1 │ new Promise(((((async () => {})))))
│ ^^^^^^^^^^^^^^
Valid
new Promise((resolve, reject) => {})
new Promise((resolve, reject) => {}, async function unrelated() {})
new Foo(async (resolve, reject) => {})
new Foo((( (resolve, reject) => {} )))