await is only valid in async function node js 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: await is only valid in async function
async function start() {
....
const result = await helper.myfunction('test', 'test');
}
Example 3: 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();