Skip to content

Latest commit

 

History

History
45 lines (31 loc) · 1019 Bytes

no-promise-reject.md

File metadata and controls

45 lines (31 loc) · 1019 Bytes

Disallow rejecting promises (functional/no-promise-reject)

This rule disallows rejecting promises.

Rule Details

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.

❌ Incorrect

/* 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;
}

✅ Correct

/* 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 };
}