Home > Web Front-end > JS Tutorial > Why Does 'await is only valid in async function' Occur in Nested JavaScript Function Calls?

Why Does 'await is only valid in async function' Occur in Nested JavaScript Function Calls?

Patricia Arquette
Release: 2024-12-09 19:56:11
Original
614 people have browsed it

Why Does

"await is only valid in async function" Exception in Nested Function Call

In a JavaScript project, you encountered an error stating "await is only valid in async function." This error arises when attempting to utilize the await keyword within a non-async function.

To resolve this issue, you must modify the outer function start to be an async function as well:

async function start(a, b) {
  const result = await helper.myfunction('test', 'test');
  console.log(result);
}
Copy after login

As start is the outer function where you intended to use await, it requires being declared as an async function to enable the use of await. This allows the outer function to properly handle and await the result of the async function myfunction within it.

The above is the detailed content of Why Does 'await is only valid in async function' Occur in Nested JavaScript Function Calls?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template