A simple, yet powerful, utility function to handle async operations, providing a clean and consistent API.
To use the utility functions with npm and/or a bundler such as webpack or rollup, install it via yarn or npm:
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).
<head>
...
<!-- as a local file -->
<script src="./your/path/to/handleAsync.browser.min.js"></script>
<!-- or via CDN -->
<script src="http://unpkg.com/@iasd/handle-async"></script>
...
</head>
The library provides exports for modern import
syntax as well as exports for the require
syntax.
// 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.
const { handleAsync } = handleAsync;
Use the handleAsync
function to handle asynchronous operations in a safe manner.
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 theOk
handler, to enforce correct handling.
If you would like to contribute, take a look at the contribution guide.
handleAsync
is licensed under the MIT License