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); }
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!