ButtonFilename match componentJsx sort propsMatch navigation routeRequire try catch asyncEnforce Error Handling in Async Functions ( require-try-catch-async )Require usestate typeValid translation key
Enforce Error Handling in Async Functions (require-try-catch-async
)
This rule enforces that all async
functions must include a try/catch
block for proper error handling. If an @safe
comment is placed directly above the function declaration or expression, the rule will skip validation.
🧩 Rule Type
- Type:
problem
- Category:
Error Handling
- Fixable: No
- Recommended: No
📚 Why Enforce Try/Catch in Async Functions?
Using try/catch
in async functions:
- Prevents unhandled promise rejections.
- Ensures predictable behavior in the presence of errors.
- Encourages developers to think explicitly about failure cases.
- Helps improve code quality and runtime resilience.
✅ Correct Usage
// With try/catchconst loadData = async () => {try {await fetchData();} catch (e) {console.error(e);}};
// With @safe comment// @safeconst loadData = async () => {await fetchData();};const loadData =// @safeasync () => {await fetchData();};// @safeconst loadData = async function () {await fetchData();};const loadData =// @safeasync function () {await fetchData();};// @safeasync function loadData() {await fetchData();}
❌ Incorrect Usage
// Missing try/catch and no @safeconst loadData = async () => {await fetchData();};
// Misspelled comment// safeconst loadData = async function () {await fetchData();};
// Misplaced commentasync function loadData() {// @safeawait fetchData();}
📂 Ignored Files
This rule applies to all JavaScript and TypeScript files. However, a function is ignored if:
- It is not marked
async
- It includes a
try/catch
block - It has an
@safe
comment directly above it
🔍 When To Enable
Enable this rule when:
- You want consistent error handling in async code.
- You want to prevent unhandled promise rejections.
- You want to enforce disciplined, defensive programming practices.
⚙️ Options
This rule does not take any configuration options.