npm.io
1.0.0 • Published 11 months ago

promise-sos

Licence
MIT
Version
1.0.0
Deps
0
Size
24 kB
Vulns
0
Weekly
0
Stars
2

Promise S.O.S

npm package npm downloads Types: TypeScript License: MIT

A lightweight, fully typed library providing utilities for working with JavaScript promises.


Features

  • Tiny and dependency-free
  • Type-safe – written in TypeScript
  • Works with ESM and CommonJS
  • Provides helpers for combining and managing promises

Installation

npm install promise-sos
# or
yarn add promise-sos

Usage

promiseAllObject

A helper function that extends Promise.all by allowing you to pass an object of promises (instead of an array). It returns a promise that resolves to an object with the same keys, but with the promises replaced by their resolved values.

Example with mixed values
import { promiseAllObject } from 'promise-sos';

const promisesObject = {
  promiseA: Promise.resolve('resolved promiseA'),
  promiseB: Promise.resolve('resolved promiseB'),
  someValue: 1234,
  flag: true
};

const result = await promiseAllObject(promisesObject);

console.log(result);
// {
//   promiseA: 'resolved promiseA',
//   promiseB: 'resolved promiseB',
//   someValue: 1234,
//   flag: true
// }
Example with rejection
import { promiseAllObject } from 'promise-sos';

const promisesObject = {
  promiseA: Promise.reject(new Error('rejected promiseA')),
  promiseB: Promise.resolve('resolved promiseB')
};

try {
  const result = await promiseAllObject(promisesObject);
} catch (error) {
  console.error(error);
  // Error: rejected promiseA
}

API

promiseAllObject<T>(promisesMap: T): Promise<PromisesMapResult<T>>
  • promisesMap: An object whose values may be promises or regular values.
  • Returns: A promise resolving to an object with the same keys, with all promises unwrapped.
  • Throws:
    • TypeError if input is not a non-empty plain object.
    • Any error from a rejected promise.

License

MIT Inaki Arroyo

Keywords