Forgetting the 'async' Keyword
You'll learn why attempting to use await without declaring a function as async results in a syntax error.
The Core Problem
While forgetting await causes runtime issues (promises that don't wait), forgetting async is caught immediately by JavaScript's parser. You cannot use await inside a regular function — the function must be declared with the async keyword. Without it, JavaScript doesn't know that the function needs to handle asynchronous operations, and it throws a syntax error before your code even runs.
Think of async as the gate pass that grants a function permission to use await. Without showing that pass, you're not allowed in.
What Happens
// ❌ SYNTAX ERROR - This won't even run
function fetchData() {
const response = await fetch('https://api.example.com/data');
return response;
}
// SyntaxError: await is only valid in async functions
The fix is simple — add the async keyword:
// ✅ Correct
async function fetchData() {
const response = await fetch('https://api.example.com/data');
return response;
}
Why This Matters
JavaScript enforces this rule to maintain clarity. The async keyword signals three things:
- This function returns a promise (even if you return a plain value)
- This function can pause execution with
await - Errors can be caught with try/catch blocks around await expressions
Without the async marker, JavaScript cannot transform your function into its promise-based execution model.
Remember the Pair
Always think of async and await as a pair: you need async on the function declaration to earn the right to use await inside it. The error message is actually helpful here — it stops you before runtime.
Key Takeaway: The async keyword is mandatory for any function that uses await; without it, you'll get a syntax error before your code executes.