Error AtlasError Documentation and Resolution

JavaScript used await outside a valid async context

SyntaxError: await is only valid in async functions, async generators and modules

JavaScript found an await expression in code that is not an async function, async generator, or top-level module context.

SyntaxError: await is only valid in async functions, async generators and modulessyntax
SyntaxError: await is only valid in async functions, async generators and modules
  • Await is used inside a non-async function.
  • Top-level await is used in code that is not treated as a module.
  • A callback or loop body was converted to async logic without changing the function signature.
  1. Mark the containing function async when appropriate.
  2. Run the file as a module if top-level await is intended.
  3. Refactor synchronous callbacks so await only appears in valid async contexts.
MDN: SyntaxError: await is only valid in async functions async generators and modules
JavaScript await is only valid in async functions: causes and fixes | Error Atlas