'javascript new Function with await doesn't seem to work but with no error message [closed]

syncTest works, await asyncTest doesn't but there's no error message, so I can't see how to fix this ?

function syncTest(a) {
  return a;
}

async function asyncTest(a) {
  return a;
}

async function test() {
  let value1 = new Function(
    `return syncTest("123");`
  )();
  console.log(value1);

try {
  let value2 = new Function(
    `return await asyncTest("123");`
  )();
  console.log(value2);
}
catch(e) {
  console.log(error.message);
}

}

test();


Solution 1:[1]

2 possible problems:

Using await most likely means the function will return a Promise, not plain text. Learn about it here, you won't regret it.

Second, the "new Function" command you are using is quite odd, and most likely isn't asynchronous in this case. The await keyword only works in asynchronous functions, E.g. (async()=>{}) is an asynchronous function.

Please take this all with a grain of salt, as I am unable to test this at the moment. Just some words of wisdom! ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 codingmaster398