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
- Host: GitHub
- URL: https://github.com/rahulc0dy/safe-execute
- Owner: rahulc0dy
- License: mit
- Created: 2025-03-20T10:51:32.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-30T08:05:18.000Z (about 1 year ago)
- Last Synced: 2025-09-04T09:00:29.496Z (8 months ago)
- Topics: cache, debounce, error-handling, function, function-caching, javascript, npm, package, throttle, typescript, utility
- Language: TypeScript
- Homepage: https://npmjs.com/package/@rahulc0dy/safe-execute
- Size: 63.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# 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.