src/content/docs/linter/rules/no-nested-promises.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.15` - Diagnostic Category: [`lint/nursery/noNestedPromises`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`promise/no-nesting`](https://github.com/eslint-community/eslint-plugin-promise/blob/main/docs/rules/no-nesting.md){
"linter": {
"rules": {
"nursery": {
"noNestedPromises": "error"
}
}
}
}
Disallow nested .then() or .catch() promise calls.
Nesting .then() or .catch() calls defeats the purpose of promises,
which is to create a flat chain of asynchronous operations. Nested promise
callbacks can make code harder to read and maintain.
However, nesting is allowed when the nested callback references variables from the outer scope, as flattening would break the code in such cases.
doThing().then(function() { return a.then() })
doThing().then(() => b.catch())
doThing()
.then(a => getB(a)
.then(b => getC(b))
)
// Simple returns
doThing().then(function() { return 4 })
doThing().then(() => 4)
// Chained promises (no nesting)
doThing()
.then(a => getB(a))
.then(b => getC(b))
// Nested but references outer scope variable 'a'
doThing()
.then(a => getB(a)
.then(b => getC(a, b))
)
// Promise.resolve/all are fine
doThing().then(function() { return Promise.all([a,b,c]) })
doThing().then(() => Promise.resolve(4))