Package Exports
- @asleepace/try
- @asleepace/try/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@asleepace/try) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Try
Type-safe error handling primitives for modern JavaScript & TypeScript projects.
const [json, error] = Try.catch(() => JSON.parse(data))
// or via async / await ...
const [user, error] = await Try.catch(fetchUser)
if (!error) {
console.log(`Hello ${user.name}!`) // Type Safe!
}Installation
Using npm:
npm install @asleepace/tryUsing Yarn:
yarn add @asleepace/tryUsing Bun:
bun add @asleepace/tryQuick Start
import { Try } from '@asleepace/try';
// Synchronous error handling
const [result, error] = Try.catch(() => {
// Your code that might throw an error
return "success";
});
if (error) {
console.error("An error occurred:", error.message);
} else {
console.log("Operation succeeded:", result);
}
// Asynchronous error handling
async function fetchData() {
const [data, error] = await Try.catch(async () => {
const response = await fetch('https://api.example.com/data');
return response.json();
});
if (error) {
console.error("Failed to fetch data:", error.message);
return null;
}
return data;
}API
Try.catch<T>(fn: () => T | Promise<T>): [T | null, Error | null] | Promise<[T | null, Error | null]>
Executes a function and returns a tuple containing either:
[result, null]if the function executes successfully[null, error]if the function throws an error
Works with both synchronous and asynchronous functions, automatically returning a Promise for async operations.
Benefits
- No Try/Catch Blocks: Clean, readable code without nested try/catch structures
- Type Safety: Full TypeScript support with proper type inference
- Consistent Pattern: Uniform error handling for both sync and async code
- Zero Dependencies: Lightweight and dependency-free
- Isomorphic: Works in both browser and Node.js environments
Testing
This package includes a comprehensive test suite. To run the tests:
# Clone the repository
git clone https://github.com/asleepace/try.git
cd try
# Install dependencies
bun install
# Run tests
bun testExamples
Error Handling in a React Component
import React, { useEffect, useState } from 'react';
import { Try } from '@asleepace/try';
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
async function loadUser() {
setLoading(true);
const [userData, fetchError] = await Try.catch(async () => {
const response = await fetch(`/api/users/${userId}`);
if (!response.ok) throw new Error(`HTTP error ${response.status}`);
return response.json();
});
setUser(userData);
setError(fetchError);
setLoading(false);
}
loadUser();
}, [userId]);
if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error.message}</div>;
return (
<div>
<h1>{user.name}</h1>
<p>{user.email}</p>
</div>
);
}Chaining Operations
import { Try } from '@asleepace/try';
async function processData(rawData) {
// Step 1: Parse the data
const [parsed, parseError] = Try.catch(() => JSON.parse(rawData));
if (parseError) return [null, new Error(`Failed to parse data: ${parseError.message}`)];
// Step 2: Transform the data
const [transformed, transformError] = Try.catch(() => {
return parsed.items.map(item => ({
id: item.id,
name: item.name.toUpperCase(),
value: item.value * 2
}));
});
if (transformError) return [null, new Error(`Failed to transform data: ${transformError.message}`)];
// Step 3: Save the data
const [saved, saveError] = await Try.catch(async () => {
const response = await fetch('/api/save', {
method: 'POST',
body: JSON.stringify(transformed)
});
return response.json();
});
if (saveError) return [null, new Error(`Failed to save data: ${saveError.message}`)];
return [saved, null];
}License
MIT