How To Check If A Function Is Async In Javascript

How to Check if a Function is Async in JavaScript

After doing some research, we also found a way to guide you to check if a function is async in JavaScript. You’ll get an approach to new things like instanceof Operator, constructor.name property. Please read the guide below to understand how we guide you.

Check if a function is async in JavaScript

Use the constructor property

The constructor property

This property will return a reference to the object constructor, the object constructor that created the instance of an object.

Example:

// Create asynchronous functions
async function asyncFunc() {
	console.log("Async Function");
}

// Check asynchronous function
if (asyncFunc.constructor.name === "AsyncFunction") {
	console.log("Function is Async");
} else {
	console.log("Function is not Async");
}

Output

Function is Async

We used “async” before the function to create the function asynchronously. The return value of the function is a Promise.

To check if a function is async in Javascript, we use the constructor.name property to compare it with the constructor name. If the constructor name is “AsyncFunction”, the above function is asynchronous and vice versa.

Use the instanceof operator

This operator will check the object type at runtime. It returns true when it indicates that the object is an instance of a particular class; it returns false if it is not.

Syntax:

objectName instanceof objectType

Parameter:

  • objectName: is the object name.

Example:

// Create AsyncFunction object
const AsyncFunction = async function() {}.constructor;

// Create asynchronous functions
async function asyncFunc() {
	console.log("Async Function");
}

// Check asynchronous function
if (asyncFunc instanceof AsyncFunction) {
	console.log("Function is Async");
} else {
	console.log("Function is not Async");
}

Output

Function is Async

In JavaScript, an asynchronous function is an AsyncFunction object.

Note that the AsyncFunction object is not a global object, but we can use the following code “async function() {}.constructor”

We then use the instanceof operator to check whether the object type is AsyncFunction at runtime. If true, the result is “Function is Async”; if false, the result is “Function is not Async”.

Use the toString() method

This method will return a string or “[object type]”.

Syntax:

object.toString()

Parameter:

  • None

Example:

// Create asynchronous functions
async function asyncFunc() {}

if (asyncFunc().toString() === "[object Promise]") {
	console.log("Function is Async");
} else {
	console.log("Function is not Async");
}

Output

Function is Async

When creating an asynchronous function, it returns a Promise. We use the toString() method to see the return object type. If it’s “[object Promise]”, the above function is asynchronous.

Summary

This article showed you several ways to check if a function is Async in JavaScript. We used constructor properties and instanceof operator to do that. If you want more tutorials like this, follow us; we constantly update the articles daily.

Maybe you are interested:

Leave a Reply

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