Files
2026-05-25 23:48:42 +02:00

2.1 KiB

Async Utils

A TypeScript library providing utility classes and functions to simplify working with asynchronous operations and promises.

Installation

yarn add github:worph/async-utils#main

Features

  • Lazy: A utility type for lazy initialization of asynchronous operations.
  • ListenerCleaner: Manages cleanup functions, allowing for easy resource management.
  • PromiseQueue: A queue system for promises, allowing for sequential execution and cancellation.
  • MultiQueue: Manages multiple PromiseQueue instances, distributing tasks to optimize concurrency.
  • Id Generation: Utility function for generating random IDs.

Usage

Lazy Initialization

import { Lazy } from "@worph/async-utils";

const lazyValue: Lazy<number> = async () => {
  // some asynchronous operation
  return 42;
};

// Usage
lazyValue().then(value => console.log(value));

Listener Cleaner

import { ListenerCleaner } from "@worph/async-utils";

const cleaner = new ListenerCleaner();
cleaner.add(() => console.log("Cleanup action"));

// Trigger all cleanup actions
cleaner.cleanUp();

Promise Queue

import { PromiseQueue } from "@worph/async-utils";

const queue = new PromiseQueue<number>();

queue.add(async () => {
  // some asynchronous task
  return 1;
});

// Wait for the queue to be empty
queue.awaitQueueEmpty().then(() => console.log("Queue empty"));

Multi Queue

import { MultiQueue } from "@worph/async-utils";

const multiQueue = new MultiQueue<number>(2); // 2 concurrent tasks

multiQueue.add(async () => {
  // some asynchronous task
  return 1;
});

// Add more tasks...

// Wait for all queues to be empty
multiQueue.awaitQueueEmpty().then(() => console.log("All queues empty"));

Generating an ID

import { makeid } from "@worph/async-utils";

const id = makeid(10); // Generates a random 10 character string
console.log(id);

Contributing

Contributions are welcome! Please submit a pull request or open an issue for discussion.

License

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