Resolves promises in key-value pairs maintaining type information.
- NodeJS 12 or later
npm i async-kv
yarn add async-kv
import { all } from "async-kv"
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.
const data = await all({
news: getNews,
music: getMusic,
movies: getMovies,
games: getGames,
})
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.
Please do!