https://github.com/dgca/result
Utility function for better error handling
https://github.com/dgca/result
Last synced: 4 months ago
JSON representation
Utility function for better error handling
- Host: GitHub
- URL: https://github.com/dgca/result
- Owner: dgca
- Created: 2026-02-24T04:17:56.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2026-02-24T07:16:18.000Z (4 months ago)
- Last Synced: 2026-02-24T13:37:51.663Z (4 months ago)
- Language: TypeScript
- Size: 51.8 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @dgca/result
A tiny utility for cleaner error handling in TypeScript. Wraps a function call and returns a `{ data, error }` tuple instead of throwing.
## Install
```bash
pnpm add @dgca/result
```
## Usage
### Async
```ts
import { result } from "@dgca/result";
const { data, error } = await result(async () => {
const response = await fetch("https://api.example.com/data");
if (!response.ok) throw new Error("Request failed");
return response.json();
});
if (error) {
console.error("Something went wrong:", error.message);
} else {
console.log("Got data:", data);
}
```
### Sync
```ts
import { result } from "@dgca/result";
const { data, error } = result(() => {
return JSON.parse(someString);
});
if (error) {
console.error("Invalid JSON:", error.message);
} else {
console.log("Parsed:", data);
}
```
### How it works
- If the function **returns** a value, you get `{ data: value, error: null }`.
- If the function **throws**, you get `{ data: null, error: Error }`.
- If something non-Error is thrown (e.g. a string), it gets wrapped in `new Error()`.
- If the function is async, `result()` returns a `Promise` — just `await` it.