You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Disallow using an async function as a Promise executor
20
+
///
21
+
/// Disallow using an async function as a Promise executor.
21
22
///
22
23
/// ### 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:
26
24
///
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:
28
28
/// ```javascript
29
29
/// const result = new Promise(function executor(resolve, reject) {
30
30
/// readFile('foo.txt', function(err, result) {
@@ -36,11 +36,46 @@ declare_oxc_lint!(
36
36
/// });
37
37
/// });
38
38
/// ```
39
-
///
40
39
/// 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
+
/// });
41
59
///
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) => {
0 commit comments