SyntaxError: await is only valid in async functions and the top level bodies of modules
SyntaxError: await is only valid in async functions and the top level bodies of modules in JavaScript means you used await inside a function that is not marked async. Every function that uses await must have the async keyword, including small callback functions.
How do I fix SyntaxError: await is only valid in async functions and the top level bodies of modules?
SyntaxError: await is only valid in async functions and the top level bodies of modules means await sits inside a function without the async keyword. Often it is a callback, such as the arrow function passed to forEach. Add async to the nearest enclosing function, or replace forEach with a for...of loop inside an async function.
- 1. Find the nearest functionLook for the closest function around the await, even a small arrow callback. That one needs async.
- 2. Replace forEachforEach does not wait. Use for (const item of items) { await save(item); } inside an async function.
- 3. Top-level await needs modulesUse <script type="module">, "type": "module" in package.json, or wrap the code in async function main().
What does SyntaxError: await is only valid in async functions and the top level bodies of modules mean?
await pauses a function until a promise finishes. JavaScript only allows that inside functions declared with async, or at the very top of an ES module. The tricky part is callbacks: if you write await inside a .forEach or .map arrow function, that inner arrow function is the one that needs async, not the outer function. Older browsers say "await is only valid in async function".
Why does SyntaxError: await is only valid in async functions and the top level bodies of modules happen?
1. A function that is missing async
Add async before function (or before the arrow's brackets).
Breaks
function loadUser() {
const res = await fetch("/api/user");
return res.json();
}Works
async function loadUser() {
const res = await fetch("/api/user");
return res.json();
}2. await inside a callback
The arrow function passed to forEach is a separate function. Use a for...of loop, which runs inside the async function.
Breaks
async function saveAll(items) {
items.forEach((item) => {
await save(item);
});
}Works
async function saveAll(items) {
for (const item of items) {
await save(item);
}
}3. Top-level await in a classic script or CommonJS file
Top-level await only works in ES modules. Wrap it in an async function, or make the file a module.
Breaks
// plain <script> or CommonJS file
const data = await fetch("/api/data");Works
async function main() {
const data = await fetch("/api/data");
}
main();How to find it in your own code
Checklist: 1) Find the await on the failing line. 2) Find the nearest function around it, even a small arrow function, and add async to that one. 3) Replace forEach with for...of when each step must wait. 4) For await at the top of a file, use <script type="module">, "type": "module" in package.json, or wrap the code in an async main() function.
Practise async and race conditions
None of the challenges raises this exact error yet. These practise the same kind of bug.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.