relike-value
Create promise from sync, async, string, number, array and so on. Handle completion (results) and errors gracefully! Built on top of
relike
, used byredolent
to build robust (hybrid) APIs.
What's the difference?
What's the difference between me and you?!
–– Dr Dre feat. Eminem & X-Zibit - Whats the Difference, https://youtu.be/8y5MjguI-pM
What's the difference between this module, relike and redolent?
–– Simply, almost nothing.
- This one accepts everything and returns
Promise
. But it is little bit tricky:
- if you pass only one non-function argument to it, it will pass it to promise
- if you pass more that one arguments to it, it will create array from them and pass it to promise
- if you pass
function
as first argument, next arguments will be passed to this function
- relike one just accept
sync
orasync
function which is executed immediately with next arguments, after that it returnsPromise
. - redolent accepts everything and returns function, which when is executed it returns
Promise
. Above things applies here, because it is on top ofrelike-value
.
Notice: Both
relike
andrelike-value
direcly executes first argument (if function) and returnsPromise
.
Install
npm i relike-value --save
Usage
For more use-cases see the tests
const relikeValue =
relikeValue
Will try to promisify
fn
with native Promise, otherwise will useBluebird
or you can give different promise module torelikeValue.promise
, for examplepinkie
.
[args...]
{Anything} any number of any type from string to function (number, array, boolean, etc)return
{Promise} promise
Example
const fs = const request = const relikeValue = // promisify sync function // handles multiple arguments by default (comes from `request`)
relikeValue.promise
Static property on which you can pass custom Promise module to use, e.g.
Q
constructor.
Example
const fs = const relikeValue = // `q` promise will be used if not native promise available// but only in node <= 0.11.12relikeValuepromise =
Access Promise constructor
You can access the used Promise constructor for promisify-ing from
promise.Prome
Example
const fs = const relikeValue = // use `pinkie` promise if not native promise available// but only in node <= 0.11.12relikeValuepromise = const promise = console//=> will be `pinkie` promise constructor (only in node <= 0.11.12)console //=> true (only on node <= 0.11.12)console //=> true (only on node <= 0.11.12) promise
Promise from any type
Any number of any type of arguments can be given - String, Stream, Null, Boolean, Number and etc.
Example
const relikeValue = var promise =
Related
- always-done: Handles completion and errors of anything!
- always-promise: Promisify, basically, everything. Generator function, callback-style or synchronous function; sync function that returns child process, stream or observable; directly passed promise, stream or child process.
- always-thunk: Thunkify, basically, everything. Generator function, callback-style or synchronous function; sync function that returns child process, stream or observable; directly passed promise, stream or child process.
- always-generator: Generatorify, basically, everything. Async, callback-style or synchronous function; sync function that returns child process, stream or observable; directly passed promise, stream or child process.
- native-or-another: Always will expose native
Promise
if available, otherwiseBluebird
but only if you don't give another promise module likeq
orpromise
or what you want. - native-promise: Get native
Promise
or falsey value if not available. - relike: Simple promisify a callback-style function with sane defaults. Support promisify-ing sync functions.
- relike-all: Promisify all functions in an object, using
relike
.
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.