Skip to content

Commit 210b876

Browse files
authoredMar 14, 2025··
docs(linter): Improve eslint-no-async-promise-executor (#9778)
Adds correctness examples and format in line with #6050
1 parent f8628bc commit 210b876

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed
 

‎crates/oxc_linter/src/rules/eslint/no_async_promise_executor.rs

+43-8
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ pub struct NoAsyncPromiseExecutor;
1717

1818
declare_oxc_lint!(
1919
/// ### What it does
20-
/// Disallow using an async function as a Promise executor
20+
///
21+
/// Disallow using an async function as a Promise executor.
2122
///
2223
/// ### Why is this bad?
23-
/// The `new Promise` constructor accepts an executor function as an argument,
24-
/// which has `resolve` and `reject` parameters that can be used to control the state of the created Promise.
25-
/// For example:
2624
///
27-
/// ### Example
25+
/// The `new Promise` constructor accepts an executor function as an argument,
26+
/// which has `resolve` and `reject` parameters that can be used to control the state of the
27+
/// created Promise. For example:
2828
/// ```javascript
2929
/// const result = new Promise(function executor(resolve, reject) {
3030
/// readFile('foo.txt', function(err, result) {
@@ -36,11 +36,46 @@ declare_oxc_lint!(
3636
/// });
3737
/// });
3838
/// ```
39-
///
4039
/// The executor function can also be an `async function`. However, this is usually a mistake, for a few reasons:
40+
/// - If an async executor function throws an error, the error will be lost and won’t cause
41+
/// the newly-constructed `Promise` to reject.This could make it difficult to debug and handle some errors.
42+
/// - If a `Promise` executor function is using `await`, this is usually a sign that it is not
43+
/// actually necessary to use the new `Promise` constructor, or the scope of the new
44+
/// `Promise` constructor can be reduced.
45+
///
46+
/// ### Examples
47+
///
48+
/// Examples of **incorrect** code for this rule:
49+
/// ```javascript
50+
/// const foo = new Promise(async (resolve, reject) => {
51+
/// readFile('foo.txt', function(err, result) {
52+
/// if (err) {
53+
/// reject(err);
54+
/// } else {
55+
/// resolve(result);
56+
/// }
57+
/// });
58+
/// });
4159
///
42-
/// - If an async executor function throws an error, the error will be lost and won’t cause the newly-constructed `Promise` to reject.This could make it difficult to debug and handle some errors.
43-
/// - If a Promise executor function is using `await`, this is usually a sign that it is not actually necessary to use the `new Promise` constructor, or the scope of the `new Promise` constructor can be reduced.
60+
/// const result = new Promise(async (resolve, reject) => {
61+
/// resolve(await foo);
62+
/// });
63+
/// ```
64+
///
65+
/// Examples of **correct** code for this rule:
66+
/// ```javascript
67+
/// const foo = new Promise((resolve, reject) => {
68+
/// readFile('foo.txt', function(err, result) {
69+
/// if (err) {
70+
/// reject(err);
71+
/// } else {
72+
/// resolve(result);
73+
/// }
74+
/// });
75+
/// });
76+
///
77+
/// const result = Promise.resolve(foo);
78+
/// ```
4479
NoAsyncPromiseExecutor,
4580
eslint,
4681
correctness

0 commit comments

Comments
 (0)