]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-promise-executor-return.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-promise-executor-return.md
1 # Disallow returning values from Promise executor functions (no-promise-executor-return)
2
3 The `new Promise` constructor accepts a single argument, called an *executor*.
4
5 ```js
6 const myPromise = new Promise(function executor(resolve, reject) {
7 readFile('foo.txt', function(err, result) {
8 if (err) {
9 reject(err);
10 } else {
11 resolve(result);
12 }
13 });
14 });
15 ```
16
17 The executor function usually initiates some asynchronous operation. Once it is finished, the executor should call `resolve` with the result, or `reject` if an error occurred.
18
19 The return value of the executor is ignored. Returning a value from an executor function is a possible error because the returned value cannot be used and it doesn't affect the promise in any way.
20
21 ## Rule Details
22
23 This rule disallows returning values from Promise executor functions.
24
25 Only `return` without a value is allowed, as it's a control flow statement.
26
27 Examples of **incorrect** code for this rule:
28
29 ```js
30 /*eslint no-promise-executor-return: "error"*/
31
32 new Promise((resolve, reject) => {
33 if (someCondition) {
34 return defaultResult;
35 }
36 getSomething((err, result) => {
37 if (err) {
38 reject(err);
39 } else {
40 resolve(result);
41 }
42 });
43 });
44
45 new Promise((resolve, reject) => getSomething((err, data) => {
46 if (err) {
47 reject(err);
48 } else {
49 resolve(data);
50 }
51 }));
52
53 new Promise(() => {
54 return 1;
55 });
56 ```
57
58 Examples of **correct** code for this rule:
59
60 ```js
61 /*eslint no-promise-executor-return: "error"*/
62
63 new Promise((resolve, reject) => {
64 if (someCondition) {
65 resolve(defaultResult);
66 return;
67 }
68 getSomething((err, result) => {
69 if (err) {
70 reject(err);
71 } else {
72 resolve(result);
73 }
74 });
75 });
76
77 new Promise((resolve, reject) => {
78 getSomething((err, data) => {
79 if (err) {
80 reject(err);
81 } else {
82 resolve(data);
83 }
84 });
85 });
86
87 Promise.resolve(1);
88 ```
89
90 ## Further Reading
91
92 * [MDN Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
93
94 ## Related Rules
95
96 * [no-async-promise-executor](no-async-promise-executor.md)