promise/no-nesting Style ​
What it does ​
Disallow nested then() or catch() statements.
Why is this bad? ​
Nesting promises makes code harder to read and understand.
Examples ​
Examples of incorrect code for this rule:
javascript
doThing().then(() => a.then());
doThing().then(function () {
a.then();
});
doThing().then(() => {
b.catch();
});
doThing().catch((val) => doSomething(val).catch(errors));
Examples of correct code for this rule:
javascript
doThing().then(() => 4);
doThing().then(function () {
return 4;
});
doThing().catch(() => 4);
javascript
doThing()
.then(() => Promise.resolve(1))
.then(() => Promise.resolve(2));
This example is not a rule violation as unnesting here would result in a
being undefined in the expression getC(a, b)
.
javascript
doThing().then((a) => getB(a).then((b) => getC(a, b)));
How to use ​
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny promise/no-nesting --promise-plugin
json
{
"plugins": ["promise"],
"rules": {
"promise/no-nesting": "error"
}
}