promise/no-nesting
Disallow then() and catch() nested inside another then() or catch()
A then() inside a then() adds a level of indentation for every step, and a catch() on the inner chain doesn't see errors from the outer one. Return the inner promise and continue the chain one level up.
A nested callback that uses a variable from the outer callback can't move up, so it isn't reported.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"promise/no-nesting" = "error" # or "warn"Examples#
example.js1 problem
getUser(id).then(user => {
return getOrders(user).thenAvoid nesting promises.(orders => render(orders));
});example.jsNo problems
getUser(id)
.then(user => getOrders(user))
.then(orders => render(orders));Here the inner callback needs user:
example.jsNo problems
getUser(id).then(user => {
return getOrders(user).then(orders => render(user, orders));
});