# maybe-try

> A declarative sync & async js maybe that notes something may fail

Latest version **1.0.12** (published 2018-03-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install maybe-try
pnpm add maybe-try
yarn add maybe-try
bun add maybe-try
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.12 |
| Published | 2018-03-24 |
| First published | 2017-10-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 134.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | cmswalker |
| Maintainers | cmswalker |
| Keywords | declarative, maybe, try, catch, try catch, fail, maybe-try, sync, synchronous, async, asynchronous, error handling, promise, callback |

## Links

- npm: https://www.npmjs.com/package/maybe-try
- Repository: https://github.com/cmswalker/maybe-try
- Homepage: https://github.com/cmswalker/maybe-try#readme
- Issues: https://github.com/cmswalker/maybe-try/issues
- npm.io page: https://npm.io/package/maybe-try

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 1.0.12 (latest) — 2018-03-24
- 1.0.11 — 2018-01-05
- 1.0.10 — 2018-01-05
- 1.0.9 — 2018-01-05
- 1.0.8 — 2018-01-05
- 1.0.7 — 2017-10-23
- 1.0.6 — 2017-10-21
- 1.0.5 — 2017-10-21
- 1.0.4 — 2017-10-21
- 1.0.3 — 2017-10-21
- 1.0.0 — 2017-10-20

## README

# maybe-try :see_no_evil:

## A declarative sync & async js maybe that notes something may fail

Node and Browser Compatible

View on [NPM](https://www.npmjs.com/package/maybe-try)

```bash
npm install maybe-try
```

## Usage

The module exposes 3 methods, one for synchronous operations, one for promises, and one for callbacks.

Each method takes 2 parameters, the fallbackValue for error scenarios, and a function.

Both parameters are **REQUIRED**, maybe-try does not supply a default value for you

Returns an object with caught errors (if any) and either the result or fallbackValue, depending on success

More in-depth examples can be found [here](https://github.com/cmswalker/maybe-try/blob/master/examples)

### Synchronous Version

```js
const maybeTry = require('maybe-try');

// Synchronous Version

const data = 'some string';
const { error, result } = maybeTry.catch(false, functionThatOperatesOnDataAsIfItWereAnArray(data));
// No need to run try/catches in your code blocks, maybeTry resolves the caught error and result with assignment
```

### Promise Version

```js
// Promise Version

function fetchReviewsForProduct(productId) {
  const fallbackValue = [{ rating: 5 }];

  return maybeTry.promise(fallbackValue, getReviewsPromise)
    .then(({ error, result }) => {
      // No need to handle catches here, maybeTry resolves both the db error and our fallback value
      // Decide what you'd like to do with the error from here, either ignore and use the fallback value, or handle it manually
      // However, you still have access to the error in the response body should you need it

      // Access both the result and the error on response
      console.log('Promise Response', result);
      return result; // fallbackValue ([{ rating: 5 }])
    });
}
```

### Callback Version

```js
// Callback Version

const cbFallbackValue = [{ name: 'Tom' }];
fetchFriendsForUser(1, maybeTry.callback(cbFallbackValue, (err, { error, result }) => {
  // No need to handle errors here, maybeTry resolves both the db error and our fallback value
  // The first argument (err) will always be null to keep with error-first callback patterns
  // However, you still have access to the error in the response body should you need it

  // Decide what you'd like to do with the error from here, either ignore and use the fallback value, or handle it manually
  // Access both the result and the error on response
  console.log('Callback Response', result);
  return result; // fallbackValue (everything's ok, at least we have Tom)
}));

```

## Registering an errorHandler

An optional feature to register a function to handle errors for all occurances within the module

It's useful if you'd like to only stick to the happy path with your application logic but send logs to an external service when errors do occur

It should be noted that the errorHandler is a fire and forget method. You cannot chain to it or call it explicitly.

### Example using the Syncronous Version

```js
const errorHandler = error => {
  const { message } = error;
  console.log('An Error was caught from maybe-try', error);
  logErrorsToAnExternalService(error);
};

maybeTry.registerErrorHandler(errorHandler);

// The API remains the same, but the errorHandler will be firing in the background
const data = 'some string';
const { error, result } = maybeTry.catch(false, functionThatOperatesOnDataAsIfItWereAnArray(data));
// at this point, logErrorsToAnExternalService() has been called
```

---
_Source: https://npm.io/package/maybe-try · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
