Categories
JavaScript Answers

How to check a function is async with JavaScript?

Spread the love

Sometimes, we want to check a function is async with JavaScript.

In this article, we’ll look at how to check a function is async with JavaScript.

How to check a function is async with JavaScript?

To check a function is async with JavaScript, we can check if the function’s constructor is an instance of the constructor that creates an async function.

For instance, we write

const AsyncFunction = (async () => {}).constructor;

console.log(asyncFn instanceof AsyncFunction)

to get the constructor of the async async () => {} function with the constructor property.

Then we check if asyncFn is an instance of the AsyncFunction constructor with asyncFn instanceof AsyncFunction.

Conclusion

To check a function is async with JavaScript, we can check if the function’s constructor is an instance of the constructor that creates an async function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *