Linter · Rules
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:
[lint.rules]
"promise/catch-or-return" = "error" # or "warn"
Every option, set to its default:
[lint.rules]
"promise/catch-or-return" = ["error", {
allowFinally = false,
allowThen = false,
allowThenStrict = false,
terminationMethod = "catch",
}]
Examples
Reported:
function refresh() {
loadItems().then(render);
}
Not reported:
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:
loadItems().then(render).catch(showError).finally(hideSpinner);
loadItems().then(render).catch(showError).finally(hideSpinner);
loadItems().then(render, showError);
With allowThenStrict, the first argument must be null:
loadItems().then(render).then(null, showError);
loadItems().then(render, showError);
terminationMethod replaces catch. List "catch" too if you want to keep it:
loadItems().then(render).done();
loadItems().then(render).catch(showError);
loadItems().then(render).catch(showError);