All Projects → slorber → combine-promises

slorber / combine-promises

Licence: MIT license
Like Promise.all(array) but with an object instead of an array.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to combine-promises

P Map
Map over promises concurrently
Stars: ✭ 639 (+253.04%)
Mutual labels:  promises, promise, await
ProtoPromise
Robust and efficient library for management of asynchronous operations in C#/.Net.
Stars: ✭ 20 (-88.95%)
Mutual labels:  promises, promise, await
Breeze
Javascript async flow control manager
Stars: ✭ 38 (-79.01%)
Mutual labels:  promises, promise, await
try-to-catch
functional try-catch wrapper for promises
Stars: ✭ 30 (-83.43%)
Mutual labels:  promise, await
bluff
🙏 Promise A+ implementation
Stars: ✭ 14 (-92.27%)
Mutual labels:  promises, promise
market-pricing
Wrapper for the unofficial Steam Market Pricing API
Stars: ✭ 21 (-88.4%)
Mutual labels:  promises, promise
Hydra
⚡️ Lightweight full-featured Promises, Async & Await Library in Swift
Stars: ✭ 1,954 (+979.56%)
Mutual labels:  promises, await
redux-reducer-async
Create redux reducers for async behaviors of multiple actions.
Stars: ✭ 14 (-92.27%)
Mutual labels:  promises, promise
WebsocketPromisify
Makes websocket's API just like REST with Promise-like API, with native Promises.
Stars: ✭ 18 (-90.06%)
Mutual labels:  promise, await
flush-promises
Flush all queued resolved promise handlers
Stars: ✭ 172 (-4.97%)
Mutual labels:  promises, await
best-queue
Queue in runtime based promise
Stars: ✭ 26 (-85.64%)
Mutual labels:  promise, await
ws-promise
A tiny, Promise-based WebSocket protocol allowing request-response usage in ECMAScript
Stars: ✭ 20 (-88.95%)
Mutual labels:  promise, await
promiviz
Visualize JavaScript Promises on the browser. Visualize the JavaScript Promise APIs and learn. It is a playground to learn about promises faster, ever!
Stars: ✭ 79 (-56.35%)
Mutual labels:  promises, promise
alls
Just another library with the sole purpose of waiting till all promises to complete. Nothing more, Nothing less.
Stars: ✭ 13 (-92.82%)
Mutual labels:  promises, promise
Q
A platform-independent promise library for C++, implementing asynchronous continuations.
Stars: ✭ 179 (-1.1%)
Mutual labels:  promises, promise
Fetch
Asynchronous HTTP client with promises.
Stars: ✭ 29 (-83.98%)
Mutual labels:  promises, promise
relaks
Asynchrounous React component
Stars: ✭ 49 (-72.93%)
Mutual labels:  promise, await
organiser
An organic web framework for organized web servers.
Stars: ✭ 58 (-67.96%)
Mutual labels:  promise, await
Tascalate Concurrent
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s
Stars: ✭ 144 (-20.44%)
Mutual labels:  promises, promise
Promise
Promise / Future library for Go
Stars: ✭ 149 (-17.68%)
Mutual labels:  promises, promise

Combine-Promises

NPM CI Size min Size minzip

Like Promise.all([]) but for objects.

import combinePromises from 'combine-promises';

const { user, company } = await combinePromises({
  user: fetchUser(),
  company: fetchCompany(),
});

Why:

  • Insensitive to destructuring order
  • Simpler async functional code

Features:

  • TypeScript support
  • Lightweight
  • Feature complete
  • Well-tested
  • ESM / CJS

Sponsor

ThisWeekInReact.com: the best newsletter to stay up-to-date with the React ecosystem:

ThisWeekInReact.com banner


Install

npm install combine-promises
// OR
yarn add combine-promises

TypeScript support

Good, native and strict TypeScript support:

  • Return type correctly inferred from the input object
  • All object values should be async
  • Only accept objects (reject arrays, null, undefined...)
const result: { user: User; company: Company } = await combinePromises({
  user: fetchUser(),
  company: fetchCompany(),
});

Insensitive to destructuring order

A common error with Promise.all is to have a typo in the destructuring order.

// Bad: destructuring order reversed
const [company, user] = await Promise.all([fetchUser(), fetchCompany()]);

This becomes more dangerous as size of the promises array grows.

With combinePromises, you are using explicit names instead of array indices, which makes the code more robust and not sensitive to destructuring order:

// Good: we don't care about the order anymore
const { company, user } = await combinePromises({
  user: fetchUser(),
  company: fetchCompany(),
});

Simpler async functional code

Suppose you have an object representing a friendship like {user1: "userId-1", user2: "userId-2"}, and you want to transform it to {user1: User, user2: User}.

You can easily do that:

import combinePromises from 'combine-promises';
import { mapValues } from 'lodash'; // can be replaced by vanilla ES if you prefer

const friendsIds = { user1: 'userId-1', user2: 'userId-2' };

const friends = await combinePromises(mapValues(friendsIds, fetchUserById));

Without this library: good luck to keep your code simple.

Inspirations

Name inspired by combineReducers from Redux.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].