Skip to content Skip to sidebar Skip to footer

Asyncfunction Is Not Defined, Yet Mdn Documents It's Usage

There is an article AsyncFunction - JavaScript on MDN. It shows the following snippet: new AsyncFunction([arg1[, arg2[, ...argN]],] functionBody) Yet in both Mozzila Firefox 55 an

Solution 1:

As mentioned in the Mozilla docs "Note that AsyncFunction is not a global object."

Therefore you can't access it as a property of the window object like other global constructors. It must be obtained by interrogating an instance of an async function:

From the docs:

constAsyncFunction = Object.getPrototypeOf(asyncfunction(){}).constructor;

Solution 2:

The documentation itself is confused so it's no wonder that people get confused as well.

MDN docs tell us that AsyncFunction is not a global object, and yet they list it in the Global Objects reference! Oops!

This leads to unexpected behavior. This works:

> f1 = function () {};
[Function: f1]
> f1 instanceofFunctiontrue

But this doesn't:

> f2 = asyncfunction () {}
[AsyncFunction: f2]
> f2 instanceofAsyncFunctionReferenceError: AsyncFunction is not defined

I wrote an unexposed module that you can use:

It basically works like the example in the answer by Patrick but you don't have to remember it.

See also this question for more info:

Post a Comment for "Asyncfunction Is Not Defined, Yet Mdn Documents It's Usage"