Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
is-async-function
Advanced tools
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.
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.
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
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 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 with npm
$ npm i is-async-function --save
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.
Example
var fs = require('fs')
var isAsyncFn = require('is-async-function')
console.log(isAsyncFunction(fs.readFile)) // => true
console.log(isAsyncFunction(fs.stat)) // => true
console.log(isAsyncFunction(fs.readFileSync)) // => false
console.log(isAsyncFunction(fs.statSync)) // => false
// or pass custom names to recognize as `async`
console.log(isAsyncFunction(fs.stat, ['cb'])) // => false
console.log(isAsyncFunction(fs.readFile, ['foo', 'bar']))
// => false, because fs.readFile uses `cb`
Params
fn
{Function}: is this fn
a callback functionnames
{Array}: arguments names, default are common-callback-namesstrict
{Boolean}: defaults to true
to always return a boolean, pass false
to get index (position) - this is useful when you wanna understand which "callback name" exists as argument in that fn
returns
{Boolean|Number}: always boolean true
or false
when on strict mode, othewise it can be Number index representing the position and if index is 0 it is transformed to boolean true
- so always positive value if function is async.non-strict mode
passing
false
as second or third argument
var isAsyncFunction = require('is-async-function')
console.log(isAsyncFunction(fs.readFile, false)) // => 2
// => 2, because it callback argument is called `cb`
// and that's the third element in `common-callback-names` array
console.log(isAsyncFunction(fs.stat, false)) // => 1
// => 1, because it callback argument is called `callback_`
// and that's the second element in `common-callback-names` array
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.
FAQs
Determine if a function is a native async function.
The npm package is-async-function receives a total of 9,918,099 weekly downloads. As such, is-async-function popularity was classified as popular.
We found that is-async-function demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.