High
Javascript
Use async/await over callbacks and .then()
Official
Rule Description
Prefer async/await for asynchronous code:
- More readable than callbacks or .then() chains
- Easier error handling with try/catch
- Works naturally with loops and conditionals
```typescript
// Good
async function fetchUserData(id: string): Promise {
try {
const response = await fetch(`/api/users/${id}`);
if (!response.ok) throw new Error('Failed to fetch');
return await response.json();
} catch (error) {
logger.error('Failed to fetch user', { id, error });
throw error;
}
}
// Avoid
function fetchUserData(id) {
return fetch(`/api/users/${id}`)
.then(res => res.json())
.catch(err => console.log(err));
}
```
Included in Rulesets
React Standards
28 rules • 1 standard
Vue Best Practices
28 rules • 1 standard
Angular Guidelines
28 rules • 1 standard
Next.js Patterns
28 rules • 1 standard
Express Standards
31 rules • 1 standard
JavaScript Guide
28 rules • 2 standards
TypeScript Guide
28 rules • 4 standards
Code Review Standards
58 rules • 0 standards
Add This Rule
Sign in to add this rule to your workspace
Sign in with GitHubDetails
- Severity
- High
- Category
- Javascript
- Used in
- 8 rulesets