SyntaxError: await is only valid in async function inside loop code example
Example 1: Uncaught SyntaxError: await is only valid in async function
const sleep = (n) => new Promise((res) => setTimeout(res, n));
const arr = [1, 2, 3];
const asyncRes = await arr.reduce(ASYNC (memo, e) => {
await sleep(10);
return (await memo) + e;
}, 0);
console.log(asyncRes);
Example 2: SyntaxError: await is only valid in async function
const myfunction = async function(x, y) {
return [
x,
y,
];
}
const start = async function(a, b) {
const result = await myfunction('test', 'test');
console.log(result);
}
start();