# try-guard

> Alternative way to handle errors in Javascript

Latest version **1.0.5** (published 2023-03-16) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2023-03-16 |
| First published | 2023-03-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Stephan Mingoes |
| Maintainers | stephanmingoes |
| Keywords | try-catch, error, handling |

## Links

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

## 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.5 (latest) — 2023-03-16
- 1.0.4 — 2023-03-16
- 1.0.3 — 2023-03-15
- 1.0.2 — 2023-03-15
- 1.0.1 — 2023-03-15
- 1.0.0 — 2023-03-15

## README

# TryGuard and TryGuardAsync

The `TryGuard` and `TryGuardAsync` functions are utility functions for handling synchronous and asynchronous operations that may throw an error. These functions catch any errors thrown by a provided function and return a tuple that contains either the result of the function or `null`, as well as either the error thrown by the function or `null`.

The `TryGuard` function is used for synchronous operations, while `TryGuardAsync` is used for asynchronous operations that return a promise.

## Usage

Both functions take a callback function and an array of arguments to be passed to the callback. The array is type-safe, meaning that TypeScript ensures that the correct types of arguments are passed to the callback function. This guarantees that you don't have to worry about passing incorrect arguments to the array, which can save you valuable time and effort in debugging your code.

Here's an example of how to use the `TryGuard` and `TryGuardAsync` functions:

```typescript
import { TryGuard, TryGuardAsync } from "try-guard";

function divideByZero(n: number) {
  return n / 0;
}

const [err, data] = TryGuard(divideByZero, [5]);

console.log(err); // Output: Error: Division by zero
console.log(data); // Output: null

async function fetchData() {
  const response = await fetch("https://example.com");
  return response.json();
}

const [asyncErr, asyncData] = await TryGuardAsync(fetchData, []);

console.log(asyncErr); // Output: null
console.log(asyncData); // Output: the parsed JSON data from the response
```

In the example above, `TryGuard` is used to handle a synchronous operation that throws an error, while `TryGuardAsync` is used to handle an asynchronous operation that returns a promise. Both functions return a tuple that contains either the result of the function or `null`, as well as either the error thrown by the function or `null`.

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