More JavaScript answers

by Danny
Published 2 min read

This error means you used await somewhere JavaScript cannot pause. Learn why, and fix it by marking the function async or loading the file as an ES module.

The short answer

#

You used await inside a function that is not marked async, or at the top of a file that is loaded as a classic script or CommonJS module. Add the async keyword to the nearest enclosing function, or load the file as an ES module so top-level await is allowed.


_10
-function loadUser(id) {
_10
+async function loadUser(id) {
_10
const response = await fetch(`/api/users/${id}`);
_10
return response.json();
_10
}

Why it happens

#

await pauses a function until a promise settles. Only two places are allowed to pause like that:

  • The body of a function declared with async (including async arrow functions and async methods).
  • The top level of an ES module, where the module loader can wait before running modules that import it.

Everywhere else, await is not a keyword the parser accepts, so the file fails to parse before any code runs. The most common causes are:

  • Forgetting async on the function, or putting it on the outer function when the await is inside an inner callback.
  • Using await inside a callback passed to forEach, map or setTimeout that is not itself async.
  • Using top-level await in a file that runs as CommonJS in Node.js, or in a browser script tag without type="module".

How to fix it

#

Mark the closest function as async

The async keyword must be on the function that directly contains the await, not on a function further out.


_10
button.addEventListener('click', async () => {
_10
const data = await fetchData();
_10
render(data);
_10
});

Fix awaits inside array callbacks

Making a forEach callback async silences the error but does not wait: forEach ignores the promises it gets back. Use a for...of loop to run one at a time, or Promise.all to run them in parallel.


_10
// One after another
_10
for (const id of ids) {
_10
await saveItem(id);
_10
}
_10
_10
// All at once
_10
await Promise.all(ids.map((id) => saveItem(id)));

Use top-level await in an ES module

In the browser, add type="module" to the script tag:


_10
<script type="module" src="/main.js"></script>

In Node.js, either name the file with a .mjs extension or set "type": "module" in the nearest package.json:


_10
{
_10
"type": "module"
_10
}

Then this works at the top of the file:


_10
const config = await loadConfig();
_10
export default config;

Wrap the code in an async function

If you cannot switch to ES modules, for example in an older CommonJS project, wrap the code in an async function and call it. Handle the rejection so errors are not lost.


_10
async function main() {
_10
const config = await loadConfig();
_10
startServer(config);
_10
}
_10
_10
main().catch((error) => {
_10
console.error(error);
_10
process.exitCode = 1;
_10
});

Common pitfalls

#
  • Adding async to the outer function when the await sits inside a nested callback. Each function needs its own async.
  • Making a forEach callback async and assuming the loop waits for it. It does not.
  • Using top-level await in a module that many others import. Every importer waits for it to finish, which can slow start-up.
  • Forgetting that the error is a parse error. Nothing in the file runs, so logs before the bad line will not appear either.
#

Was this resource helpful?