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.
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:
async
only when you actually need to await
somethingawait
keywords that don’t add valueExample 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.
Enter the URL of a public GitHub repository