Skip to content

Resolve parallel promises in key-value pairs whilst maintaining type information

License

Notifications You must be signed in to change notification settings

tonytamps/async-kv

Repository files navigation

async-kv

Resolves promises in key-value pairs maintaining type information.

Prerequisites

  • NodeJS 12 or later

Installation

npm i async-kv
yarn add async-kv

Usage

import { all } from "async-kv"

all

Assumes all values are functions that return promises and resolves them in parallel.

Since this is using Promise.all under the hood it follows the same behaviour.

This returned promise will resolve when all of the input's promises have resolved, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.

Basic

const data = await all({
  news: getNews,
  music: getMusic,
  movies: getMovies,
  games: getGames,
})

With parameters

const data = await all({
  news: getNews,
  music: () => getMusic("optional", "parameters"),
  movies: getMovies,
  games: getGames,
})

The really important information here is that the return types of the functions are mapped back to the data object and Typescript can continue knowing they've all been resolved.

Contributing

Please do!

Credits

License

MIT License

About

Resolve parallel promises in key-value pairs whilst maintaining type information

Resources

License

Stars

Watchers

Forks

Packages

No packages published