Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fizzygalacticus/promise-or-not
https://github.com/fizzygalacticus/promise-or-not
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/fizzygalacticus/promise-or-not
- Owner: FizzyGalacticus
- Created: 2019-08-28T01:59:08.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-02-12T13:08:39.000Z (almost 3 years ago)
- Last Synced: 2024-11-09T15:45:52.714Z (10 days ago)
- Language: JavaScript
- Size: 106 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](https://github.com/FizzyGalacticus/savethis) project.For synchronous functions:
```jsconst 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:
```jsconst 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](https://github.com/FizzyGalacticus/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.