An open API service indexing awesome lists of open source software.

https://github.com/rahulc0dy/safe-execute

npm install @rahulc0dy/safe-execute
https://github.com/rahulc0dy/safe-execute

cache debounce error-handling function function-caching javascript npm package throttle typescript utility

Last synced: 3 months ago
JSON representation

npm install @rahulc0dy/safe-execute

Awesome Lists containing this project

README

          



GitHub package.json version (branch)


GitHub package.json version (branch)


GitHub package.json version (branch)


GitHub package.json version (branch)


GitHub package.json version (branch)




Issues


GitHub Stars


CodeRabbit Reviews




Test Status


Build Status

# Safe Execute

Safe Execute is a versatile utility library that simplifies the execution of asynchronous tasks while handling errors gracefully. It not only provides core functions like tryCatch and safeExecute, but also includes three additional utilities—throttle, debounce, and cache—to improve performance and control.

## Installation

You can add Safe Execute to your project using npm or pnpm:

- Using `npm`:

```bash
npm install @rahulc0dy/safe-execute
```

- Using `pnpm`:

```bash
pnpm add @rahulc0dy/safe-execute
```

## Usage

### Core Functions

- **tryCatch**
Execute a promise safely and get either the data or error without try/catch blocks.

- **safeExecute**
Run synchronous or asynchronous functions with built-in error handling and optional callbacks.

### Additional Utility Functions

- **throttle**
Rate-limit a function so it executes at most once per defined interval.

- **debounce**
Delay function execution until a period of inactivity, ideal for inputs and filtering.

- **cache**
Cache the result of asynchronous calls to prevent redundant execution.

For detailed usage examples and API descriptions, refer to the sections below or check the documentation within your IDE.

## API Overview

### tryCatch

**Signature:**

```typescript
async function tryCatch(
promise: Promise
): Promise<{ data: T | null; error: E | null }>;
```

- On success: `{ data: T, error: null }`
- On failure: `{ data: null, error: E }`

### safeExecute

**Signature:**

```typescript
async function safeExecute(
fn: () => Promise | T,
options?: {
onSuccess?: (result: T) => void;
onError?: (error: unknown) => void;
timeoutMs?: number;
}
): Promise<{
data: T | null;
isError: boolean;
isSuccess: boolean;
isLoading: boolean;
error: unknown;
}>;
```

Executes a given function safely, handling both synchronous and asynchronous operations, with optional callbacks and a timeout.

### throttle

**Signature:**

```typescript
function throttle any>(fn: T, wait: number): T;
```

Returns a throttled version of the function that only executes once within the specified wait time.

### debounce

**Signature:**

```typescript
function debounce any>(fn: T, delay: number): T;
```

Returns a debounced version of the function that delays execution until after the delay period has passed without further invocation.

### cache

**Signature:**

```typescript
function cache Promise>(fn: T): T;
```

Caches the result of asynchronous function calls to avoid redundant executions when called with the same arguments.

## Contributing

Contributions are welcome! Please refer to our [contributing guidelines](CONTRIBUTING.md) for details on how to help improve the project.

## License

This project is licensed under the [MIT License](LICENSE).

## Support

If you encounter any issues or have questions, please open an [issue](https://github.com/rahulc0dy/safe-execute/issues) for assistance.