Skip to main content

Installation

Install Trybox using your favorite package manager:
npm install trybox

Basic Usage

Trybox provides a simple way to handle async operations safely.

Using run directly

For simple use cases, you can use the run function directly:
import { run } from "trybox";

const result = await run(
  () => fetch("https://api.example.com/data").then((res) => res.json()),
  {
    retries: 3,
    timeout: 5000,
  }
);

if (result.ok) {
  console.log("Success:", result.data);
} else {
  console.error("Failed:", result.error);
}

Using trybox

For more advanced configurations and reuse, create a Runner instance:
import trybox from "trybox";

// Create a reusable runner with default configuration
const runner = trybox({
  retries: 2,
  timeout: 10000,
  // Global error handling rules
  onError: (error) => {
    // Log to your monitoring service
    console.error("Global error handler:", error);
  },
});

// Use it across your application
const result = await runner.run(() => complexOperation());

Next Steps

Now that you have Trybox installed, explore these key features: