How to Check if a Function is Async in JavaScript


Borislav Hadzhiev

Last updated: Oct 30, 2021


Photo from Unsplash

Check if a Function is Async in JavaScript #

To check if a function is async, access the property on the function and check if the value is equal to AsyncFunction, e.g. === 'AsyncFunction'. If the equality check returns true, then the function is async.

const sum = async (a, b) => { return a + b; }; if ( === 'AsyncFunction') { // 👇️ this runs console.log('✅ function is async'); } else { console.log('⛔️ function is NOT async'); }

When access on a function, the constructor property returns:

Every async function in JavaScript is an AsyncFunction object.

The last step is to access the name property on the constructor and check if the AsyncFunction constructor was used to create the function.

If the condition passes, then we have an async function.

This approach works for arrow and named functions.

If we access the name property on a non-async function, the string "Function" is returned.

function example() {} console.log(; // 👉️ "Function"

Different constructor functions are used to create async and non-async functions, so we can easily determine if a function is async.

Keep in mind that a non-async function might return a promise.
// 👇️ non-async returns Promise function example() { return new Promise(resolve => { resolve(100); }); } console.log(example()); // 👉️ Promise {} console.log(; // 👉️ "Function"

The example shows a non-async function, which returns a promise.

The only way to know if a non-async function returns a promise is to invoke the function.

You can check if the return value of a function is a promise in the following way.

function example() { return new Promise(resolve => { resolve(100); }); } function isPromise(p) { if (typeof p === 'object' && typeof p.then === 'function') { return true; } return false; } console.log(isPromise(example())); // 👉️ true console.log(isPromise(() => {})); // 👉️ false

We check if the passed in value is an object and it contains a property named then that is of type function.

This approach would fail is someone passed an object with a then property of type function that is not actually a promise.

Most of the time you can't just call functions to check what their return value is because they might be mutating state, e.g. in a database.

However, if you could call the function without mutating any state, you can combine these 2 conditions.

if ( === 'AsyncFunction' || (typeof example === 'function' && isPromise(example())) ) { console.log('✅ Function returns promise'); } else { console.log('⛔️ Function does NOT return promise'); }

Our if statement checks if the function is async or it returns a promise. If either check passes, the if block runs.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.