A tiny helper that makes Promise.all a bit more useful by:
- Accepting a mapper function as a second argument
- Resolving an object of promises too
npm install promise-all-map
import all from 'promise-all-map';
all(promises, [mapper])
const promises = [
fetchPost(1),
fetchPost(2),
fetchPost(3),
];
const titles = await all(promises, (post) => post.title);
titles // ['post title 1', 'post title 2', 'post title 3']
all(array, [mapper])
const titles = await all([1,2,3], async (id) => {
const posts = await fetchPost(id)
return posts.title;
});
titles // ['post title 1', 'post title 2', 'post title 3']
all(object, [mapper])
const posts = await all({
one: fetchPost(1),
two: fetchPost(2),
three: fetchPost(3),
});
posts.one // { title: 'post title 1' }