]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/max-nested-callbacks.md
first commit
[pve-eslint.git] / eslint / docs / rules / max-nested-callbacks.md
1 # enforce a maximum depth that callbacks can be nested (max-nested-callbacks)
2
3 Many JavaScript libraries use the callback pattern to manage asynchronous operations. A program of any complexity will most likely need to manage several asynchronous operations at various levels of concurrency. A common pitfall that is easy to fall into is nesting callbacks, which makes code more difficult to read the deeper the callbacks are nested.
4
5 ```js
6 foo(function () {
7 bar(function () {
8 baz(function() {
9 qux(function () {
10
11 });
12 });
13 });
14 });
15 ```
16
17 ## Rule Details
18
19 This rule enforces a maximum depth that callbacks can be nested to increase code clarity.
20
21 ## Options
22
23 This rule has a number or object option:
24
25 * `"max"` (default `10`) enforces a maximum depth that callbacks can be nested
26
27 **Deprecated:** The object property `maximum` is deprecated; please use the object property `max` instead.
28
29 ### max
30
31 Examples of **incorrect** code for this rule with the `{ "max": 3 }` option:
32
33 ```js
34 /*eslint max-nested-callbacks: ["error", 3]*/
35
36 foo1(function() {
37 foo2(function() {
38 foo3(function() {
39 foo4(function() {
40 // Do something
41 });
42 });
43 });
44 });
45 ```
46
47 Examples of **correct** code for this rule with the `{ "max": 3 }` option:
48
49 ```js
50 /*eslint max-nested-callbacks: ["error", 3]*/
51
52 foo1(handleFoo1);
53
54 function handleFoo1() {
55 foo2(handleFoo2);
56 }
57
58 function handleFoo2() {
59 foo3(handleFoo3);
60 }
61
62 function handleFoo3() {
63 foo4(handleFoo4);
64 }
65
66 function handleFoo4() {
67 foo5();
68 }
69 ```
70
71 ## Further Reading
72
73 * [Control flow in Node.js](http://book.mixu.net/node/ch7.html)
74 * [Control Flow in Node](https://howtonode.org/control-flow)
75 * [Control Flow in Node Part II](https://howtonode.org/control-flow-part-ii)
76
77 ## Related Rules
78
79 * [complexity](complexity.md)
80 * [max-depth](max-depth.md)
81 * [max-len](max-len.md)
82 * [max-lines](max-lines.md)
83 * [max-lines-per-function](max-lines-per-function.md)
84 * [max-params](max-params.md)
85 * [max-statements](max-statements.md)