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.
You might also be interested in always-done.
(TOC generated by verb using markdown-toc)
Install with npm
$ npm install is-async-function --save
or install using yarn
$ yarn add is-async-function
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 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.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`
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
Side note: In previous nodejs versions it was called in a few different ways - cb_
, callback_
and etc. That's why common-callback-names exists. As in v7 it seems everything now is called callback
. So in most of the cases you will get boolean true
always - both in strict and non-strict mode. In non-strict mode that will mean your function has argument called callback
.
If you pass array of names as second argument, in non-strict mode you will get index of that array.
Example
var isAsyncFn = require('is-async-function')
// you considered you callback fucntion
// to be called `qux` for some reason
function myAsyncFn (foo, bar, qux) {
qux(null, 123)
}
console.log(isAsyncFn(myAsyncFn)) // => false
console.log(isAsyncFn(myAsyncFn, false)) // => false
console.log(isAsyncFn(myAsyncFn, ['callback', 'qux'], false)) // => 1
// you are getting "1", because `qux` is second item
// in provided `names` array.
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guidelines for advice on opening issues, pull requests, and coding standards.
If you need some help and can spent some cash, feel free to contact me at CodeMentor.io too.
In short: If you want to contribute to that project, please follow these things
npm run commit
to commit changes instead of git commit
, because it is interactive and user-friendly. It uses commitizen behind the scenes, which follows Conventional Changelog idealogy.npm run release
, which is standard-version and follows Conventional Changelog idealogy.Thanks a lot! :)
Documentation and that readme is generated using verb-generate-readme, which is a verb generator, so you need to install both of them and then run verb
command like that
$ npm install verbose/verb#dev verb-generate-readme --global && verb
Please don't edit the README directly. Any changes to the readme must be made in .verb.md.
Clone repository and run the following in that cloned directory
$ npm install && npm test
Charlike Mike Reagent
Copyright © 2015, 2017, Charlike Mike Reagent. Released under the MIT License.
This file was generated by verb-generate-readme, v0.4.3, on March 11, 2017.
Project scaffolded using charlike cli.
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.