@fizzygalacticus/promise-or-not

2.0.0 • Public • Published

promise-or-not

promise-or-not is a function decorator that can act as a middleman to perform operations on resulting data, even if the function initially returns a promise.

Installation

yarn add @fizzygalacticus/promise-or-not || npm i --save @fizzygalacticus/promise-or-not

Usage

Parameters:

  • fn - The primary function you wish to run
  • onData - The function to call when data is received
  • onError - The function to call when an error occurrs
  • returnPromise - Forces even synchronous functions to return a Promise. This can be useful in situations where you may want to make multiple calls to promise-or-not as I do in my savethis project.

For synchronous functions:

const promiseOrNot = require('@fizzygalacticus/promise-or-not');

const mySyncFn = i => 'some return value ' + i;

const decorated = promiseOrNot(mySyncFn, val => console.log(val));

const val = decorated(1); // will print `1`
console.log(val) // `1`

For asynchronous functions:

const promiseOrNot = require('@fizzygalacticus/promise-or-not');

const mySyncFn = i => Promise.resolve('some return value ' + i);

const decorated = promiseOrNot(mySyncFn, val => console.log(val));

decorated(1).then(console.log); // will print `1` twice

Why?

This was initially written for my trythis library, which just prints the return value of a wrapped function (such as the example above). I then realized that this would be very handy to abstract away for other, potentially similar use cases.

Package Sidebar

Install

npm i @fizzygalacticus/promise-or-not

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

9.96 kB

Total Files

9

Last publish

Collaborators

  • fizzygalacticus