await outside async 🟨 JavaScript

SyntaxError: await is only valid in async functions and the top level bodies of modules

await was used inside a function that isn’t async (or in a classic, non-module script).

Seen on: JavaScript

Meaning

Callbacks (forEach, event listeners, array methods) are separate functions — they must themselves be async.

Common causes

  • Missing async on the enclosing function
  • await inside forEach/map callback
  • Top-level await in a non-module script or CommonJS

⚡ Quick fix

  1. Mark the function async
  2. Use for…of instead of forEach for sequential awaits
  3. Use type="module" or "type": "module" for top-level await

Detailed fix by platform

JavaScript

  1. javascript
    for (const id of ids) {
      await save(id);
    }

How to diagnose

  1. Function — Which function contains the await?
  2. Context — Module or classic script?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.