|
| 1 | +# [is-async-function][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url] [![npm downloads][downloads-img]][downloads-url] |
| 2 | + |
| 3 | +> 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. |
| 4 | +
|
| 5 | +[![code climate][codeclimate-img]][codeclimate-url] [![standard code style][standard-img]][standard-url] [![travis build status][travis-img]][travis-url] [![coverage status][coveralls-img]][coveralls-url] [![dependency status][david-img]][david-url] |
| 6 | + |
| 7 | +## Install |
| 8 | +> Install with [npm](https://www.npmjs.com/) |
| 9 | +
|
| 10 | +``` |
| 11 | +$ npm i is-async-function --save |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | +> For more use-cases see the [tests](./test.js) |
| 16 | +
|
| 17 | +```js |
| 18 | +const isAsyncFunction = require('is-async-function') |
| 19 | +``` |
| 20 | + |
| 21 | +## API |
| 22 | + |
| 23 | +### [isAsyncFunction](index.js#L47) |
| 24 | +> Trying to guess is `fn` asynchronous function or not. But not [is-callback-function][] be aware of that diff. |
| 25 | +
|
| 26 | +**Example** |
| 27 | + |
| 28 | +```js |
| 29 | +var fs = require('fs') |
| 30 | +var isAsyncFn = require('is-async-function') |
| 31 | + |
| 32 | +console.log(isAsyncFunction(fs.readFile)) // => true |
| 33 | +console.log(isAsyncFunction(fs.stat)) // => true |
| 34 | + |
| 35 | +console.log(isAsyncFunction(fs.readFileSync)) // => false |
| 36 | +console.log(isAsyncFunction(fs.statSync)) // => false |
| 37 | + |
| 38 | +// or pass custom names to recognize as `async` |
| 39 | +console.log(isAsyncFunction(fs.stat, ['cb'])) // => false |
| 40 | +console.log(isAsyncFunction(fs.readFile, ['foo', 'bar'])) |
| 41 | +// => false, because fs.readFile uses `cb` |
| 42 | +``` |
| 43 | + |
| 44 | +**Params** |
| 45 | + |
| 46 | +* `fn` **{Function}**: is this `fn` a callback function |
| 47 | +* `names` **{Array}**: arguments names, default are [common-callback-names][] |
| 48 | +* `strict` **{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` |
| 49 | +* `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. |
| 50 | + |
| 51 | +**non-strict mode** |
| 52 | + |
| 53 | +> passing `false` as second or third argument |
| 54 | +
|
| 55 | +```js |
| 56 | +var isAsyncFunction = require('is-async-function') |
| 57 | + |
| 58 | +console.log(isAsyncFunction(fs.readFile, false)) // => 2 |
| 59 | +// => 2, because it callback argument is called `cb` |
| 60 | +// and that's the third element in `common-callback-names` array |
| 61 | + |
| 62 | +console.log(isAsyncFunction(fs.stat, false)) // => 1 |
| 63 | +// => 1, because it callback argument is called `callback_` |
| 64 | +// and that's the second element in `common-callback-names` array |
| 65 | +``` |
| 66 | + |
| 67 | +## Related |
| 68 | +- [common-callback-names](https://www.npmjs.com/package/common-callback-names): List of common callback names - callback, cb, callback_, next, done. | [homepage](https://github.com/tunnckocore/common-callback-names#readme "List of common callback names - callback, cb, callback_, next, done.") |
| 69 | +- [fn-args](https://www.npmjs.com/package/fn-args): Get the arguments of a function | [homepage](https://github.com/sindresorhus/fn-args "Get the arguments of a function") |
| 70 | +- [fn-name](https://www.npmjs.com/package/fn-name): Get the name of a named function | [homepage](https://github.com/sindresorhus/fn-name "Get the name of a named function") |
| 71 | +- [function-arguments](https://www.npmjs.com/package/function-arguments): Get arguments of a function, useful for and used in dependency injectors… [more](https://github.com/tunnckocore/function-arguments#readme) | [homepage](https://github.com/tunnckocore/function-arguments#readme "Get arguments of a function, useful for and used in dependency injectors. Works for regular functions, generator functions and arrow functions.") |
| 72 | +- [get-fn-name](https://www.npmjs.com/package/get-fn-name): Get function name with strictness and correctness in mind. Also works for… [more](https://github.com/tunnckocore/get-fn-name#readme) | [homepage](https://github.com/tunnckocore/get-fn-name#readme "Get function name with strictness and correctness in mind. Also works for arrow functions and getting correct name of bounded functions. Powered by [fn-name][].") |
| 73 | +- [is-callback-function](https://www.npmjs.com/package/is-callback-function): Returns true if function is a callback. Checks its name is one… [more](https://github.com/tunnckocore/is-callback-function#readme) | [homepage](https://github.com/tunnckocore/is-callback-function#readme "Returns true if function is a callback. Checks its name is one of [common-callback-names][] - callback, cb, cb_, callback_, next, done, they can be customized, these are default.") |
| 74 | +- [parse-function](https://www.npmjs.com/package/parse-function): Parse a function, arrow function or string to object with name, args… [more](https://github.com/tunnckocore/parse-function#readme) | [homepage](https://github.com/tunnckocore/parse-function#readme "Parse a function, arrow function or string to object with name, args, params and body properties.") |
| 75 | + |
| 76 | +## Contributing |
| 77 | +Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/tunnckoCore/is-async-function/issues/new). |
| 78 | +But before doing anything, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) guidelines. |
| 79 | + |
| 80 | +## [Charlike Make Reagent](http://j.mp/1stW47C) [![new message to charlike][new-message-img]][new-message-url] [![freenode #charlike][freenode-img]][freenode-url] |
| 81 | + |
| 82 | +[![tunnckoCore.tk][author-www-img]][author-www-url] [![keybase tunnckoCore][keybase-img]][keybase-url] [![tunnckoCore npm][author-npm-img]][author-npm-url] [![tunnckoCore twitter][author-twitter-img]][author-twitter-url] [![tunnckoCore github][author-github-img]][author-github-url] |
| 83 | + |
| 84 | +[common-callback-names]: https://github.com/tunnckocore/common-callback-names |
| 85 | +[function-arguments]: https://github.com/tunnckocore/function-arguments |
| 86 | +[is-callback-function]: https://github.com/tunnckocore/is-callback-function |
| 87 | + |
| 88 | +[npmjs-url]: https://www.npmjs.com/package/is-async-function |
| 89 | +[npmjs-img]: https://img.shields.io/npm/v/is-async-function.svg?label=is-async-function |
| 90 | + |
| 91 | +[license-url]: https://github.com/tunnckoCore/is-async-function/blob/master/LICENSE |
| 92 | +[license-img]: https://img.shields.io/npm/l/is-async-function.svg |
| 93 | + |
| 94 | +[downloads-url]: https://www.npmjs.com/package/is-async-function |
| 95 | +[downloads-img]: https://img.shields.io/npm/dm/is-async-function.svg |
| 96 | + |
| 97 | +[codeclimate-url]: https://codeclimate.com/github/tunnckoCore/is-async-function |
| 98 | +[codeclimate-img]: https://img.shields.io/codeclimate/github/tunnckoCore/is-async-function.svg |
| 99 | + |
| 100 | +[travis-url]: https://travis-ci.org/tunnckoCore/is-async-function |
| 101 | +[travis-img]: https://img.shields.io/travis/tunnckoCore/is-async-function/master.svg |
| 102 | + |
| 103 | +[coveralls-url]: https://coveralls.io/r/tunnckoCore/is-async-function |
| 104 | +[coveralls-img]: https://img.shields.io/coveralls/tunnckoCore/is-async-function.svg |
| 105 | + |
| 106 | +[david-url]: https://david-dm.org/tunnckoCore/is-async-function |
| 107 | +[david-img]: https://img.shields.io/david/tunnckoCore/is-async-function.svg |
| 108 | + |
| 109 | +[standard-url]: https://github.com/feross/standard |
| 110 | +[standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg |
| 111 | + |
| 112 | +[author-www-url]: http://www.tunnckocore.tk |
| 113 | +[author-www-img]: https://img.shields.io/badge/www-tunnckocore.tk-fe7d37.svg |
| 114 | + |
| 115 | +[keybase-url]: https://keybase.io/tunnckocore |
| 116 | +[keybase-img]: https://img.shields.io/badge/keybase-tunnckocore-8a7967.svg |
| 117 | + |
| 118 | +[author-npm-url]: https://www.npmjs.com/~tunnckocore |
| 119 | +[author-npm-img]: https://img.shields.io/badge/npm-~tunnckocore-cb3837.svg |
| 120 | + |
| 121 | +[author-twitter-url]: https://twitter.com/tunnckoCore |
| 122 | +[author-twitter-img]: https://img.shields.io/badge/[email protected] |
| 123 | + |
| 124 | +[author-github-url]: https://github.com/tunnckoCore |
| 125 | +[author-github-img]: https://img.shields.io/badge/[email protected] |
| 126 | + |
| 127 | +[freenode-url]: http://webchat.freenode.net/?channels=charlike |
| 128 | +[freenode-img]: https://img.shields.io/badge/freenode-%23charlike-5654a4.svg |
| 129 | + |
| 130 | +[new-message-url]: https://github.com/tunnckoCore/ama |
| 131 | +[new-message-img]: https://img.shields.io/badge/ask%20me-anything-green.svg |
| 132 | + |
| 133 | +[fn-name]: https://github.com/sindresorhus/fn-name |
0 commit comments