0

I am trying to define an AsyncFunction (please note the big F) in node as described here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction

But I get:

AsyncFunction('var1', 'var2', 'let test=var1+var2') (1, 2)
ReferenceError: AsyncFunction is not defined

Creating a normal Function object works fine. Does NodeJS not support AsyncFunction?

Daniele Testa
  • 1,538
  • 3
  • 16
  • 34

1 Answers1

1

I think the question you're fundamentally asking is what is the syntax for defining an asynchronous function, and not really trying to invoke the constructor of that object.

if so then:

synchronous:

function foo()

asynchronous:

async function bar()

Update: After understanding your comments, I encourage you to re-read the spec: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction

It clearly describes that it is not a global-scope object. The examples listed below it as well, also describe it's usage which also reinforce the idea that it must be assigned (locally) before usage, so there is a definition of it.

G-Force
  • 345
  • 3
  • 10
  • 1
    No, I am not. I am asking why AsyncFunction was not working in Node. I needed to manually assign it first. const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor; – Daniele Testa Sep 14 '21 at 15:10
  • @DanieleTesta Why do you want to use the `AsyncFunction` constructor anyway? It's not a global object specifically because you're expected to use the syntax shown in this answer to create async functions. – Lennholm Sep 14 '21 at 15:14
  • @Lennholm Because the code I want to run is inside a variable. If I define it the "normal" way, I will have to use eval() to execute the code and then async does not work anyways. – Daniele Testa Sep 14 '21 at 15:16