Skip to content

A JS module allow dispatch multiple promise and manage them

License

Notifications You must be signed in to change notification settings

doublelam/promises-delivery

Repository files navigation

promises-delivery

This package is used to manage multiple promises by giving a key, you can call resolve or reject function out of the promise parameter callback and manage them by a key.

Install

yarn add promises-delivery

Usage

index.ts

import Delivery from 'promises-delivery';

// Initialize with default timeout
const delivery = new Delivery<string>({ timeout: 1000 });

// Register a promise with custom timeout
const promise = delivery.register('uniqueKey', { timeout: 2000 });

// Resolve the promise later
delivery.resolve('uniqueKey', 'value');

// Or reject it
delivery.reject('uniqueKey', new Error('something went wrong'));

// Get an existing promise
const existingPromise = delivery.getPromise('uniqueKey');

Methods

{
    register: (key: string) => Promise<T>
    resolve: (key: string, value: T) => void;
    reject: (key: string, reason: string) => void
}

Error Handling

The library provides a custom DeliveryError class and error codes for different scenarios:

import Delivery, { DeliveryError, DeliveryErrorCode, isDeliveryError } from 'promises-delivery';

const delivery = new Delivery({ timeout: 1000 });

try {
  await delivery.register('key');
  // or delivery.resolve(key, value);
  // or delivery.reject(key, value);
} catch (error) {
  if (isDeliveryError(error)) {
    switch (error.code) {
      case DeliveryErrorCode.Timeout:
        console.log('Promise timed out');
        break;
      case DeliveryErrorCode.PromiseNotFound:
        console.log('Promise not found');
        break;
      case DeliveryErrorCode.PromiseAlreadyRegistered:
        console.log('Promise already registered');
        break;
    }
  }
}

Error Types

Error Code Description
TIMEOUT Promise execution exceeded timeout limit
PROMISE_NOT_FOUND Attempted to access a non-existent promise
PROMISE_ALREADY_REGISTERED Attempted to register a duplicate key

Contribution

yarn install
yarn tsc

About

A JS module allow dispatch multiple promise and manage them

Resources

License

Stars

Watchers

Forks

Packages

No packages published