JSPM

async-utils-sdk

1.0.1
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 6
    • Score
      100M100P100Q56806F
    • License MIT

    A TypeScript-based set of asynchronous utility functions.

    Package Exports

      This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (async-utils-sdk) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

      Readme

      Async Utils SDK

      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.

      Installation

      Using npm:

      npm install async-utils-sdk

      Using yarn:

      yarn add async-utils-sdk

      After installation, you can import and use the utilities in your project.

      Features

      • 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.

      Usage

      1. Debounce

      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();

      Parameters:

      • fn: The async function to debounce.
      • wait: The time to wait in milliseconds after the last invocation before calling fn.

      Returns:

      • A debounced version of the fn function.

      2. Throttle

      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();

      Parameters:

      • fn: The async function to throttle.
      • wait: The time to wait between invocations.

      Returns:

      • A throttled version of the fn function.

      3. Retry

      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);

      Parameters:

      • fn: The async function to retry.
      • retries: The number of retry attempts.
      • delay: The time to wait between retries in milliseconds.

      Returns:

      • The result of fn if successful, or an error if all retries fail.

      4. Memoize

      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)

      Parameters:

      • fn: The async function to memoize.

      Returns:

      • A memoized version of the fn function.

      5. Async Map

      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]

      Parameters:

      • array: The array to iterate over.
      • fn: The async function to apply to each element.

      Returns:

      • A promise that resolves to an array of results.

      6. Async ForEach

      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

      Parameters:

      • array: The array to iterate over.
      • fn: The async function to apply to each element.

      How to Contribute

      Contributions are always welcome! If you'd like to contribute, please follow the steps below:

      1. Fork the repository.
      2. Create a new branch (git checkout -b feature/your-feature).
      3. Make your changes.
      4. Commit your changes (git commit -m 'Add feature').
      5. Push to your branch (git push origin feature/your-feature).
      6. Open a pull request.

      License

      This project is licensed under the MIT License - see the LICENSE file for details.

      Contact

      Versioning

      We use SemVer for versioning. For the available versions, see the tags on this repository.