Skill Details
Runtime
(claude-4-6-sonnet)
Example user message
[userMessage]: function getUser(id) { fetch('/api/users/' + id) .then(res => res.json()) .then(data => { return data }) } const user = getUser(5) console.log(user.name)
Example agent response
STEP 1 — ANALYSIS
Language: JavaScript
Purpose: Fetch user data by ID
Complexity: Simple, but has a
critical async bug
STEP 2 — BUGS FOUND
Bug 1 — CRITICAL:
Location: getUser function
Issue: Function doesn't return the
promise, and caller doesn't await it
Why it's a bug: `getUser(5)` returns
undefined immediately — the fetch
hasn't resolved yet when
console.log runs
Impact: `user.name` will throw
"Cannot read property of undefined"
STEP 3 — CODE SMELLS
- No error handling on fetch call
- No handling for non-200 responses
- Function name doesn't indicate
it's asynchronous
STEP 4 — REFACTORED CODE
```javascript
async function getUser(id) {
try {
const res = await fetch(`/api/users/${id}`);
if (!res.ok) {
throw new Error(`HTTP error: ${res.status}`);
}
const data = await res.json();
return data;
} catch (error) {
console.error('Failed to fetch user:', error);
throw error;
}
}
// Usage - must await the async function
async function main() {
try {
const user = await getUser(5);
console.log(user.name);
} catch (error) {
console.error('Could not load user:', error);
}
}
main();
```
STEP 5 — CHANGE SUMMARY
| Change | Reason | Impact |
|--------|--------|--------|
| Added async/await | Original didn't wait for promise | Fixes undefined return bug |
| Added try/catch | No error handling existed | Prevents unhandled promise rejections |
| Added res.ok check | Fetch doesn't throw on 404/500 | Catches failed requests properly |
| Template literal for URL | String concatenation is less readable | Improves readability |
| Wrapped usage in async main() | Top-level code can't use await directly (unless module) | Makes usage pattern correct |
STEP 6 — TEST SUGGESTIONS
1. Test successful fetch returns
correct user object
2. Test fetch with invalid ID
returns proper error
3. Test network failure is caught
and logged, not crashing app
4. Test with res.ok = false
(e.g. 404) throws expected error
5. Edge case: test with id =
undefined/null
Suggested test file:
getUser.test.js using Jest with
mocked fetch responses
Note: Test this in your actual
environment before deploying —
review does not replace running
your test suite.
By purchasing this skill, you agree to our terms of service
CLAUDE-4-6-SONNET
A structured 6-step code refactoring and
bug-fixing agent for developers using
Cursor.
Analyzes your code, identifies bugs,
code smells, and performance issues,
then delivers refactored code with
explanations, test suggestions, and
a before/after comparison.
Works with JavaScript, Python, TypeScript,
and React codebases. Paste your code and
get a complete review plus fixed version
instantly.
...more
Added 2 weeks ago
