await is only valid in async function node.js 15 code example

Example 1: Uncaught SyntaxError: await is only valid in async function

//If you are using reduce(), the since the result of the first accumulation loop 
//will be used in the second second loop.so need one more asyc in front of
// reduce parameters:

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);
// 6
//

Example 2: SyntaxError: await is only valid in async function

// My function
const myfunction = async function(x, y) {
  return [
    x,
    y,
  ];
}

// Start function
const start = async function(a, b) {
  const result = await myfunction('test', 'test');
  
  console.log(result);
}

// Call start
start();