https://github.com/iamsebastiandev/handleasync
Rust Style Error Handling for TypeScript
https://github.com/iamsebastiandev/handleasync
async error-handling typescript
Last synced: 5 months ago
JSON representation
Rust Style Error Handling for TypeScript
- Host: GitHub
- URL: https://github.com/iamsebastiandev/handleasync
- Owner: IamSebastianDev
- License: mit
- Created: 2023-05-10T21:37:24.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-01T19:23:43.000Z (over 1 year ago)
- Last Synced: 2025-03-24T16:31:48.556Z (over 1 year ago)
- Topics: async, error-handling, typescript
- Language: TypeScript
- Homepage:
- Size: 950 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- Contributing: contributing.md
- License: license.md
Awesome Lists containing this project
README
# @iasd/handle-async
A simple, yet powerful, utility function to handle async operations, providing a clean and consistent API.
## 🚀 Getting started
To use the utility functions with npm and/or a bundler such as webpack or rollup, install it via yarn or npm:
```bash
yarn add @iasd/handle-async
# or use npm
npm install @iasd/handle-async
```
You can also use it directly in the browser and include it via CDN (or locally, if you like).
```html
...
...
```
## 🔧 Usage
### Imports and Global
The library provides exports for modern `import` syntax as well as exports for the `require` syntax.
```js
// node require syntax
const { handleAsync } = require('@iasd/handle-async');
// modern es6 style syntax
import { handleAsync } from '@iasd/handle-async';
```
In case you included the library file locally or via CDN, the `handleAsync` object is globally available.
```js
const { handleAsync } = handleAsync;
```
### Handling Async Calls
Use the `handleAsync` function to handle asynchronous operations in a safe manner.
```ts
import { handleAsync } from '@iasd/handle-async';
// Create a async runner that returns a boolean
const asyncRunner = () => new Promise((res) => window.setTimeout(() => res(true), 5000));
// Retrieve the result from the runner
const result = await handleAsync(asyncRunner, {
Ok: (value) => value, // Use the `Ok` handler to return or transform the value
Err: (err) => false, // Use the `Err` handler to handle any error or rethrow if necessary
});
// Result will be either `true`, if the runner encountered no issue,
// or `false` if it was handled by the error handler.
console.log(result);
```
> Tip: The value returned by the `Err` handler must be of the same type as the value returned by the `Ok` handler, to enforce correct handling.
## Contributing
If you would like to contribute, take a look at the [contribution guide](./contributing.md).
## License
`handleAsync` is licensed under the [MIT License](https://opensource.org/licenses/MIT)