relike
Simple promisify async or sync function with sane defaults. Lower level than
promisify
thing. Can be used to createpromisify
method.
You might also be interested in hybridify, hybridify-all, relike-all, letta
Install
npm i relike --save
Usage
For more use-cases see the tests
const relike =
Note: It treat functions as asynchronous, based on is-async-function.
Why you should be aware of that? Because if you give async function which don't have last argument called with some of the common-callback-names it will treat that function as synchronous and things may not work as expected.
It's not a problem for most of the cases and for node's native packages, because that's a convention.
So, the relike-all package successfuly can promisifies all of the fs
functions for example, except fs.createReadStream
and fs.createWriteStream
which is normal.
relike
Runs
fn
in native Promise if available, or another provided inrelike.Promise
. If not given and not support for native Promise, it will use bluebird promise, but only on that enviroments that don't have support.
Params
<fn>
{Function}: Some async or synchronous function.[...args]
{Mixed}: Any number of any type of arguments, they are passed tofn
.returns
{Promise}: Always native Promise if supported on enviroment.
Example
const fs = const request = const relike = // handles multiple arguments by default (comes from `request`)
.promisify
Thin wrapper function around
relike()
. It accepts a function and returns a function, which when is invoked returns aPromise
. Just like any other.promisify
method, for exampleBluebird.promisify
.
Params
fn
{Function}: Some sync or async function to promisify.[Promize]
{Function}: Promise constructor to be used on enviroment where no support for native.returns
{Function}: Promisified function, which always return a Promise.
Example
var fs = var relike = var readFile = relike // also can promisify sync functionvar statFile = relike
.Promise
While relike
always trying to use native Promise if available in the enviroment, you can
give a Promise constructor to be used on enviroment where there's no support - for example, old
broswers or node's 0.10 version. By default, relike
will use and include bluebird on old enviroments,
as it is the fastest implementation of Promises. So, you are able to give Promise constructor, but
it won't be used in modern enviroments - it always will use native Promise, you can't trick that. You
can't give custom promise implementation to be used in any enviroment.
Example
var fs = var relike = relikepromisifyPromise = // using `Q` promise on node 0.10var readFile = relike
One way to pass a custom Promise constructor is as shown above. But the other way is passing it to .Promise
of the promisified function, like that
var fs = var relike = var statFile = relike statFilePromise = // using `when` promise on node 0.10
One more thing, is that you can access the used Promise and can detect what promise is used. It is easy, just as promise.Promise
and you'll get it.
Or look for promise.___bluebirdPromise
and promise.___customPromise
properties. .___bluebirdPromise
(yea, with three underscores in front) will be true if enviroment is old and you didn't provide promise constructor to .Promise
.
So, when you give constructor .__customPromise
will be true and .___bluebirdPromise
will be false.
var fs = var relike = var promise = promise console // => used Promise constructorconsole // => `true` on old env, falsey otherwiseconsole // => `true` when pass `.Promise`, falsey otherwise
Or finally, you can pass Promise constructor as second argument to .promisify
method. Like that
const fs = const relike = const readFile = relike const promise = console // => The `when` promise constructor, on old enviromentsconsole // => `true` on old environments
Related
- callback2stream: Transform sync, async or generator function to Stream. Correctly handle errors. homepage
- hybridify: Create sync or async function to support both promise and callback-style APIs… more | homepage
- letta: Promisify sync, async or generator function, using relike. Kind of promisify, but… more | homepage
- limon: The pluggable JavaScript lexer. Limon = Lemon. | homepage
- promise2stream: Transform ES2015 Promise to Stream - specifically, Transform Stream using… more | homepage
- relike-all: Promisify all functions in an object, using
relike
. | homepage - use-ware: Adds sync plugin support to your application. Kinda fork of use -… more | homepage
- use: Easily add plugin support to your node.js application. | homepage
- value2stream: Transform any value to stream. Create a stream from any value -… more | homepage
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.