How to Check if a Function is Async in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Check if a Function is Async in JavaScript

To check if a function is async, access the constructor.name property on the function and check if the value is equal to AsyncFunction.

If the comparison returns true, then the function is async.

index.js
const sum = async (a, b) => { return a + b; }; if (sum.constructor.name === 'AsyncFunction') { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… function is async'); } else { console.log('โ›”๏ธ function is NOT async'); }

check if function is async

The code for this article is available on GitHub

When accessed 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.

index.js
function example() {} console.log(example.constructor.name); // ๐Ÿ‘‰๏ธ "Function"

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

If you have to do this often, define a reusable function.

index.js
function isAsyncFunction(func) { return func.constructor.name === 'AsyncFunction'; } const sum = async (a, b) => { return a + b; }; const subtract = (a, b) => { return a - b; }; console.log(isAsyncFunction(sum)); // ๐Ÿ‘‰๏ธ true console.log(isAsyncFunction(subtract)); // ๐Ÿ‘‰๏ธ false

define reusable function

The code for this article is available on GitHub

The function takes another function as a parameter and returns true if the supplied function is async and false otherwise.

# Non-async functions might also return a Promise

Keep in mind that a non-async function might return a promise.

index.js
// ๐Ÿ‘‡๏ธ non-async returns Promise function example() { return new Promise(resolve => { resolve(100); }); } console.log(example()); // ๐Ÿ‘‰๏ธ Promise {} console.log(example.constructor.name); // ๐Ÿ‘‰๏ธ "Function"

non async function might also return promise

The example shows a non-async function that returns a promise.

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

# Check if a Function returns a Promise in JavaScript

To check if a function returns a Promise:

  1. Check if the function is async.
  2. Alternatively, invoke the function and check if it returns a promise.
  3. If either condition is met, the function returns a promise.
index.js
// โœ… Promise check function isPromise(p) { if (typeof p === 'object' && typeof p.then === 'function') { return true; } return false; } // โœ… Check if a function's return value is a Promise function returnsPromise(f) { if ( f.constructor.name === 'AsyncFunction' || (typeof f === 'function' && isPromise(f())) ) { console.log('โœ… Function returns promise'); return true; } console.log('โ›”๏ธ Function does NOT return promise'); return false; } // ๐Ÿ‘‡๏ธ Examples async function exampleAsync() {} function example() {} function examplePromise() { return new Promise(resolve => { resolve(42); }); } console.log(returnsPromise(exampleAsync)); // ๐Ÿ‘‰๏ธ true console.log(returnsPromise(example)); // ๐Ÿ‘‰๏ธ false console.log(returnsPromise(examplePromise)); // ๐Ÿ‘‰๏ธ true
The code for this article is available on GitHub

The first function in the code sample checks if the passed-in value is a promise.

index.js
function isPromise(p) { if (typeof p === 'object' && typeof p.then === 'function') { return true; } return false; }

The second function takes another function as a parameter and checks if its return value is a promise.

index.js
function returnsPromise(f) { if ( f.constructor.name === 'AsyncFunction' || (typeof f === 'function' && isPromise(f())) ) { console.log('โœ… Function returns promise'); return true; } console.log('โ›”๏ธ Function does NOT return promise'); return false; }
The code for this article is available on GitHub

The first condition in the if statement checks if the function is async.

Every async function returns a Promise, so if the function is async, we know that it returns a promise.

Our second condition checks if the passed-in value is a function and invokes it, passing the result to the isPromise() function.

The only way to check if a non-async function returns a promise is to invoke it. Note that it might not be safe to invoke the function if it mutates state, e.g. writes to a database.

There are two ways to check if a function returns a promise:

  1. Check if the function is async - then it returns a promise 100% of the time.
  2. Check if the function's return value is an object that has a property then of type function.

An alternative approach is to not check if the function returns a Promise, but to use the Promise.resolve() method instead.

# Guarantee that the function always returns a Promise

You can use the Promise.resolve() method to guarantee that the function will always return a Promise.

index.js
function example() { return 42; } Promise.resolve(example()).then(value => { console.log(value); // ๐Ÿ‘‰๏ธ 42 });

guarantee that the function always returns a promise

The code for this article is available on GitHub

The function doesn't return a Promise, however, we used the Promise.resolve() method to wrap the value in a Promise.

The code sample also works if the function returns a Promise.

index.js
async function example() { return 42; } Promise.resolve(example()).then(value => { console.log(value); // ๐Ÿ‘‰๏ธ 42 });

also works if function returns promise

We marked the function as async and async functions always return a Promise.

Either way, our code works as expected.

The Promise.resolve() method resolves the supplied value to a Promise.

If the provided value is a Promise, then the Promise is returned, otherwise, the method returns a Promise that is resolved with the supplied value.

If the value is not a Promise, we wrap it in one to be able to use the .then() method.

If the value is a Promise, it gets returned as is and we are still able to use the .then() method.

I've also written an article on how to access the value of a Promise.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev