1.0.0 • Published 8 years ago

promise-fn v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

promise-fn

Build status NPM version XO code style

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 arguments
  • promise-fncall - A Function.call that can deal with promise arguments
  • promise-if - The if statement (or unary operator) for promises
  • promise-or - The OR (||) operator for promises
  • promise-and - The AND (&&) operator for promises
  • promise-not - The NOT (!) operator for promises
  • promise-all - A Promise.all but for Objects as well as Arrays
  • promise-get - Get a property from the object a promise resolves to, using a dot path

API

promiseFn(fn, thisArg)

NameTypeDescription
fnFunctionFunction to wrap
thisArgAnyThe value to be passed as this when calling fn

Returns: Function, the function to call instead of the original function.

License

MIT © Joakim Carlstein