Prompt
Be deliberate about when to use the async keyword and await expressions. Making a function async changes its behavior - it will always return a Promise, even for early returns. This can break conditional logic that checks for synchronous vs asynchronous behavior.
Consider these patterns:
- Use
asynconly when you actually need toawaitsomething - For functions with early returns that should be synchronous, return Promise.all() or similar constructs directly instead of using async/await
- Remove unnecessary
awaitkeywords that don’t add value
Example from the codebase:
// Avoid: Always returns Promise due to async, breaks early return logic
const executeHead = async () => {
if (!match) {
return // Still returns Promise<undefined>
}
// ... rest of function
}
// Prefer: Early returns are synchronous, later returns are Promise
const executeHead = () => {
if (!match) {
return // Returns undefined directly
}
return Promise.all([
// async operations here
])
}
This approach preserves the ability to use patterns like if('then' in result) to detect whether a function returned a Promise or a synchronous value.