promise-fn
v1.0.0
Published
Wrap a function to be able to call it with promises as arguments, i.e. use promises as if they where non-promise values
Downloads
4
Maintainers
Readme
promise-fn
Wrap a function to be able to call it with promises as arguments, i.e. use promises as if they where non-promise values
Installation
Install promise-fn
using npm:
npm install --save promise-fn
Usage
Module usage
const fn = require('promise-fn');
// Create a logging function which can log data from promises as well as non-promise values:
const log = fn(console.log);
// Create a JSON parser which can parse strings from promises as well as non-promise values:
const parse = fn(JSON.parse);
const getAsyncData = () => new Promise(resolve => setTimeout(resolve, 1000, '{"hello": "world"}'));
const getSyncData = () => '{"hi": "there"}';
log('data:', parse(getAsyncData()), parse(getSyncData()));
// data: { hello: 'world' } { hi: 'there' }
// will be printed to the console in about 1 second
Related packages
promise-fnapply
- A Function.apply that can deal with promise argumentspromise-fncall
- A Function.call that can deal with promise argumentspromise-if
- The if statement (or unary operator) for promisespromise-or
- The OR (||) operator for promisespromise-and
- The AND (&&) operator for promisespromise-not
- The NOT (!) operator for promisespromise-all
- A Promise.all but for Objects as well as Arrayspromise-get
- Get a property from the object a promise resolves to, using a dot path
API
promiseFn(fn, thisArg)
| Name | Type | Description |
|------|------|-------------|
| fn | Function
| Function to wrap |
| thisArg | Any
| The value to be passed as this
when calling fn
|
Returns: Function
, the function to call instead of the original function.
License
MIT © Joakim Carlstein