A TypeScript-based collection of utility functions for asynchronous programming, including features like debouncing, retrying, and caching of promises. Designed to simplify and optimize asynchronous code in JavaScript and TypeScript applications.
npm install async-utils-sdk
yarn add async-utils-sdk
After installation, you can import and use the utilities in your project.
- Debounce: Delay execution of a function until after the specified time has passed since the last invocation.
- Throttle: Limit the rate at which a function is executed, ensuring it is only called once within a specified time period.
- Retry: Retry a function (e.g., API request) a specified number of times if it fails.
- Memoize: Cache the result of an asynchronous function to avoid redundant calculations.
- Async Map: Map an asynchronous function over an array of items.
- Async ForEach: Iterate over an array asynchronously, running a function for each item in the array.
The debounce
function delays the execution of a function until after the specified time has passed since the last invocation.
import { debounce } from 'async-utils-sdk';
const fetchData = async () => {
console.log('Fetching data...');
};
const debouncedFetch = debounce(fetchData, 2000);
// This will only log 'Fetching data...' once after 2 seconds, no matter how many times it's called
debouncedFetch();
debouncedFetch();
debouncedFetch();
-
fn
: The async function to debounce. -
wait
: The time to wait in milliseconds after the last invocation before callingfn
.
- A debounced version of the
fn
function.
The throttle
function limits how often a function can be invoked to once every specified period.
import { throttle } from 'async-utils-sdk';
const logData = async () => {
console.log('Logging data...');
};
const throttledLog = throttle(logData, 1000);
// This will log 'Logging data...' only once per second, even if it's called multiple times
throttledLog();
throttledLog();
throttledLog();
-
fn
: The async function to throttle. -
wait
: The time to wait between invocations.
- A throttled version of the
fn
function.
The retry
function allows you to retry a function multiple times if it fails.
import { retry } from 'async-utils-sdk';
const fetchData = async () => {
console.log('Trying to fetch data...');
// Simulate an API call that might fail
if (Math.random() < 0.5) {
throw new Error('Failed');
}
return 'Data fetched';
};
const retryFetch = retry(fetchData, 3, 1000);
retryFetch().then(console.log).catch(console.error);
-
fn
: The async function to retry. -
retries
: The number of retry attempts. -
delay
: The time to wait between retries in milliseconds.
- The result of
fn
if successful, or an error if all retries fail.
The memoize
function caches the result of a function, preventing it from being called again with the same arguments.
import { memoize } from 'async-utils-sdk';
const calculate = async (x: number) => {
console.log('Calculating...');
return x * 2;
};
const memoizedCalculate = memoize(calculate);
memoizedCalculate(5).then(console.log); // Logs: 'Calculating...', 10
memoizedCalculate(5).then(console.log); // Logs: 10 (no calculation)
-
fn
: The async function to memoize.
- A memoized version of the
fn
function.
The asyncMap
function maps an asynchronous function over an array.
import { asyncMap } from 'async-utils-sdk';
const double = async (x: number) => x * 2;
asyncMap([1, 2, 3], double).then(console.log); // Logs: [2, 4, 6]
-
array
: The array to iterate over. -
fn
: The async function to apply to each element.
- A promise that resolves to an array of results.
The asyncForEach
function iterates over an array asynchronously.
import { asyncForEach } from 'async-utils-sdk';
const logItem = async (item: number) => console.log(item);
asyncForEach([1, 2, 3], logItem); // Logs: 1, 2, 3
-
array
: The array to iterate over. -
fn
: The async function to apply to each element.
Contributions are always welcome! If you'd like to contribute, please follow the steps below:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature
). - Make your changes.
- Commit your changes (
git commit -m 'Add feature'
). - Push to your branch (
git push origin feature/your-feature
). - Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Author: Your Name
- Email: your-email@example.com
- GitHub: https://github.com/yourusername
We use SemVer for versioning. For the available versions, see the tags on this repository.