This rule disallows rejecting promises.
It is useful when using an Option
type (something like { value: T } | { error: Error }
)
for handling errors. In this case a promise should always resolve with an Option
and never reject.
/* eslint functional/no-promise-reject: "error" */
async function divide(x, y) {
const [xv, yv] = await Promise.all([x, y]);
return yv === 0
? Promise.reject(new Error("Cannot divide by zero."))
: xv / yv;
}
/* eslint functional/no-promise-reject: "error" */
async function divide(x, y) {
const [xv, yv] = await Promise.all([x, y]);
return yv === 0
? { error: new Error("Cannot divide by zero.") }
: { value: xv / yv };
}