How to Check if a Function is Async in JavaScript

avatar

Borislav Hadzhiev

Sat Oct 30 20212 min read

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, e.g. myFunction.constructor.name === 'AsyncFunction'. If the equality check 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'); }

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.

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.

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"

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.

index.js
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.

index.js
if ( example.constructor.name === '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 #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee