1.0.5 • Published 6 years ago

promise-all-map v1.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

promise-all-map

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

Install

  npm install promise-all-map

Usage

  import all from 'promise-all-map';

Works as 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']

An array with async mapper:

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']

Resolving an object of promises:

all(object, [mapper])

const posts = await all({
  one: fetchPost(1),
  two: fetchPost(2),
  three: fetchPost(3),
});

posts.one // { title: 'post title 1' }
1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago