'tsnode with new AsyncFunction gives error
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
const fn = new AsyncFunction('a','await blah')
So I'm trying to create a function using new AsyncFunction
This is working perfectly on the server. But when I do tsnode script.ts, I got
SyntaxError: await is only valid in async functions and the top level bodies of modules
for the exact line.
Somehow tsnode is creating a normal function instead of an async one. Is there any trick around this?
Solution 1:[1]
In my case, the exact fn defintion in string is:
`try{ ${js}\n return await ${fnName}(${args}) \n}catch(err){console.log('err ',err)}`
So if I get rid of await, it works ie
`try{ ${js}\n return ${fnName}(${args}) \n}catch(err){console.log('err ',err)}`
Still doesn't resolve the main tsnode async-fn creation problem, but this is a trick that unstucks me
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 | Archy Will He ??? |
