promise/no-nesting Style
它的作用
不允许嵌套的 then() 或 catch() 语句。
为什么这很糟糕?
嵌套 Promise 会使代码更难阅读和理解。
示例
此规则的错误代码示例:
javascript
doThing().then(() => a.then());
doThing().then(function () {
a.then();
});
doThing().then(() => {
b.catch();
});
doThing().catch((val) => doSomething(val).catch(errors));此规则的正确代码示例:
javascript
doThing().then(() => 4);
doThing().then(function () {
return 4;
});
doThing().catch(() => 4);javascript
doThing()
.then(() => Promise.resolve(1))
.then(() => Promise.resolve(2));这个示例不构成规则违反,因为如果在这里取消嵌套, 表达式 getC(a, b) 中的 a 将变为未定义。
javascript
doThing().then((a) => getB(a).then((b) => getC(a, b)));如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["promise"],
"rules": {
"promise/no-nesting": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["promise"],
rules: {
"promise/no-nesting": "error",
},
});bash
oxlint --deny promise/no-nesting --promise-plugin版本
此规则于 v0.15.13 中添加。
