Skip to content

eslint/max-nested-callbacks Pedantic ​

What it does ​

Enforce a maximum depth that callbacks can be nested. This rule helps to limit the complexity of callback nesting, ensuring that callbacks do not become too deeply nested, improving code readability and maintainability.

Why is this bad? ​

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 is nesting callbacks excessively, making code harder to read and understand.

Examples ​

Examples of incorrect code for this rule with the { "max": 3 } option:

js
foo1(function () {
  foo2(function () {
    foo3(function () {
      foo4(function () {
        // ...
      });
    });
  });
});

Examples of correct code for this rule with the { "max": 3 } option:

js
foo1(handleFoo1);

function handleFoo1() {
  foo2(handleFoo2);
}

function handleFoo2() {
  foo3(handleFoo3);
}

function handleFoo3() {
  foo4(handleFoo4);
}

function handleFoo4() {
  foo5();
}

Options ​

max ​

{ type: number, default: 10 }

The max enforces a maximum depth that callbacks can be nested.

Example:

json
"eslint/max-nested-callbacks": ["error", 10]

"eslint/max-nested-callbacks": [
  "error",
  {
    max: 10
  }
]

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny max-nested-callbacks
json
{
  "rules": {
    "max-nested-callbacks": "error"
  }
}

References ​

Released under the MIT License.