What is is-async-function?
The is-async-function npm package is designed to help developers determine if a given function is an asynchronous function. This can be particularly useful in scenarios where the behavior of code needs to be altered based on whether a function is asynchronous or not, such as in dynamic function execution, middleware handling, or in libraries that need to support both synchronous and asynchronous operations.
What are is-async-function's main functionalities?
Checking if a function is asynchronous
This feature allows you to check if a given function is an async function. The package exports a single function that takes one argument (the function to check) and returns a boolean indicating whether the function is asynchronous. This is useful for dynamically handling functions based on their nature.
const isAsyncFunction = require('is-async-function');
async function foo() {};
console.log(isAsyncFunction(foo)); // true
function bar() {};
console.log(isAsyncFunction(bar)); // false
Other packages similar to is-async-function
is-promise
The is-promise package checks if a value is a Promise, which is a related but different check compared to is-async-function. While is-async-function checks the nature of the function itself, is-promise checks the return value of a function to determine if it's a Promise. This distinction is crucial in understanding and handling asynchronous operations in JavaScript.
kind-of
kind-of is a more general utility for checking the type of a value in JavaScript, which includes support for identifying functions and promises among many other types. Compared to is-async-function, kind-of offers a broader range of type checks but does not specifically focus on distinguishing between synchronous and asynchronous functions. It's more versatile but less specialized.
Is function really asynchronous function? Trying to guess that based on check if common-callback-names exists as function arguments names or you can pass your custom.
Install
npm i is-async-function --save
Usage
For more use-cases see the tests
const isAsyncFunction = require('is-async-function')
Trying to guess is fn
asynchronous function or not. But not is-callback-function be aware of that diff.
Params
fn
{Function}: Is this fn
a callback function.names
{Array}: Arguments names, default are common-callback-names.returns
{Boolean}
Example
var fs = require('fs')
var isAsyncFn = require('is-async-function')
console.log(isAsyncFunction(fs.readFile))
console.log(isAsyncFunction(fs.stat))
console.log(isAsyncFunction(fs.readFileSync))
console.log(isAsyncFunction(fs.statSync))
console.log(isAsyncFunction(fs.stat, ['cb']))
console.log(isAsyncFunction(fs.readFile, ['callback', 'next']))
Related
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.