noNestedPromises
Summary
Section titled “Summary”- Rule available since:
v2.3.15 - Diagnostic Category:
lint/suspicious/noNestedPromises - This rule isn’t recommended, so you need to enable it.
- This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
promise/no-nesting
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "suspicious": { "noNestedPromises": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”doThing().then(function() { return a.then() })code-block.js:1:38 lint/suspicious/noNestedPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Avoid nesting promises.
> 1 │ doThing().then(function() { return a.then() })
│ ^^^^
2 │
ℹ Nesting promises can lead to harder-to-read code because it creates multiple levels of indentation and makes the flow of asynchronous operations less clear.
ℹ Consider refactoring the code to use promise chaining (foo.then().then()) instead of nesting.
doThing().then(() => b.catch())code-block.js:1:24 lint/suspicious/noNestedPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Avoid nesting promises.
> 1 │ doThing().then(() => b.catch())
│ ^^^^^
2 │
ℹ Nesting promises can lead to harder-to-read code because it creates multiple levels of indentation and makes the flow of asynchronous operations less clear.
ℹ Consider refactoring the code to use promise chaining (foo.then().then()) instead of nesting.
doThing() .then(a => getB(a) .then(b => getC(b)) )code-block.js:3:6 lint/suspicious/noNestedPromises ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Avoid nesting promises.
1 │ doThing()
2 │ .then(a => getB(a)
> 3 │ .then(b => getC(b))
│ ^^^^
4 │ )
5 │
ℹ Nesting promises can lead to harder-to-read code because it creates multiple levels of indentation and makes the flow of asynchronous operations less clear.
ℹ Consider refactoring the code to use promise chaining (foo.then().then()) instead of nesting.
// Simple returnsdoThing().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 finedoThing().then(function() { return Promise.all([a,b,c]) })doThing().then(() => Promise.resolve(4))Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.