max-nested-callbacks
强制执行回调可以嵌套的最大深度
许多 JavaScript 库使用回调模式来管理异步操作。任何复杂度的程序很可能需要在不同的并发级别管理多个异步操作。一个容易陷入的常见陷阱是嵌套回调,这会使代码随着回调层级的加深而变得更难阅读。
🌐 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.
foo(function () {
bar(function () {
baz(function() {
qux(function () {
});
});
});
});
规则详情
🌐 Rule Details
此规则强制执行回调可以嵌套的最大深度以提高代码清晰度。
🌐 This rule enforces a maximum depth that callbacks can be nested to increase code clarity.
选项
🌐 Options
此规则有一个数字或对象选项:
🌐 This rule has a number or object option:
"max"(默认10)强制执行回调可以嵌套的最大深度
已弃用: 对象属性 maximum 已被弃用;请改用对象属性 max。
max
使用 { "max": 3 } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "max": 3 } option:
/*eslint max-nested-callbacks: ["error", 3]*/
foo1(function() {
foo2(function() {
foo3(function() {
foo4(function() {
// Do something
});
});
});
});
使用 { "max": 3 } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "max": 3 } option:
/*eslint max-nested-callbacks: ["error", 3]*/
foo1(handleFoo1);
function handleFoo1() {
foo2(handleFoo2);
}
function handleFoo2() {
foo3(handleFoo3);
}
function handleFoo3() {
foo4(handleFoo4);
}
function handleFoo4() {
foo5();
}
相关规则
版本
此规则是在 ESLint v0.2.0 中引入。