promise/prefer-catch
Require catch() instead of a second argument to then()
The second argument of then(a, b) only handles a rejection from before the then(). If a itself throws, b isn't called. A catch() after the then() handles both.
The rule only reports a then() call with two arguments when it's a statement by itself. One that's returned, awaited, assigned, or followed by more of the chain isn't reported.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"promise/prefer-catch" = "error" # or "warn"Examples#
example.js1 problem
loadItems().then(render, showError)Prefer catch to then(a, b) or then(null, b); // an error in render is not handledexample.js1 problem
loadItems().then(null, showError)Prefer catch to then(a, b) or then(null, b);example.jsNo problems
loadItems().then(render).catch(showError);
loadItems().catch(showError);example.jsNo problems
function refresh() {
return loadItems().then(render, showError);
}