promise/catch-or-return
Require a promise chain to end with catch() or to be returned
When a promise chain is a statement by itself, no other code can handle its errors. Without a catch() at the end, a failure becomes an unhandled rejection.
The rule checks statements that call then(), catch(), finally() or a static method such as Promise.all(). A chain that's returned, awaited or assigned isn't reported, because the code that receives it can handle the error. Cypress chains that start with cy. aren't reported either.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"promise/catch-or-return" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"promise/catch-or-return" = ["error", {
allowFinally = false,
allowThen = false,
allowThenStrict = false,
terminationMethod = "catch",
}]Examples#
example.js1 problem
function refresh() {
loadItems().then(render)Expected catch or return.;
}example.jsNo problems
function refresh() {
loadItems().then(render).catch(showError);
}
function refreshLater() {
return loadItems().then(render);
}
async function refreshNow() {
await loadItems().then(render);
}
cy.get("button").then(click);Options#
| Option | Default | What it does |
|---|---|---|
allowFinally | false | Allow finally() after the catch(). |
allowThen | false | Allow then(a, b) with two arguments as the end of a chain. |
allowThenStrict | false | Allow then(null, b) as the end of a chain. |
terminationMethod | "catch" | The method, or a list of methods, that a chain must end with. |
By default, the catch() must come last, so this is reported:
example.js1 problem
loadItems().then(render).catch(showError).finally(hideSpinner)Expected catch or return.;example.jswith
{ "allowFinally": true }No problemsloadItems().then(render).catch(showError).finally(hideSpinner);example.jswith
{ "allowThen": true }No problemsloadItems().then(render, showError);With allowThenStrict, the first argument must be null:
example.jswith
{ "allowThenStrict": true }No problemsloadItems().then(render).then(null, showError);example.jswith
{ "allowThenStrict": true }1 problemloadItems().then(render, showError)Expected catch or return.;terminationMethod replaces catch. List "catch" too if you want to keep it:
example.jswith
{ "terminationMethod": ["catch", "done"] }No problemsloadItems().then(render).done();
loadItems().then(render).catch(showError);example.jswith
{ "terminationMethod": "done" }1 problemloadItems().then(render).catch(showError)Expected done or return.;