npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promise-map-limit

v1.0.0

Published

Map an array n items at a time with promises

Downloads

1,579

Readme

promise-map-limit

Map an array n items at a time with promises

API

mapLimit(iterable, limit, iteratee)

Parameters:

  • iterable The collection to iterate over. The iterator interface will be used if it is implemented. If the collection does not implement the iterator interface then Object.keys() is used and values are iterated in the order of the keys.
  • limit The concurrency limit. The maximum number of async operations at a time.
  • iteratee The function that will be called with each value in iterable. It can return a Promise or a plain value if no async operation is needed. If the iteratee function throws then mapLimit aborts and rejects with the error.

Returns:

mapLimit returns a Promise<Array> that resolves with the transformed values from iterable. If iteratee throws or returns a rejected promise then mapLimit will abort and return a rejected promise with the same error.

Example

const mapLimit = require('promise-map-limit');

// This will work on max two items in the array at a time
// When all items are processed it will resolve with the resulting array in the same order.
mapLimit([1, 2, 3, 4], 2, n => Promise.resolve(n * 2)).then(array => {
  // array == [2, 4, 6, 8]
});

// The iterator function can return the result synchronous
mapLimit([1, 2, 3, 4], 2, n => n * 2).then(array => {
  // array == [2, 4, 6, 8]
});

// Any value that implements the iterator interface can be used, such as Array, String, Map, Set
mapLimit('abc', 2, str => str.toUpperCase()).then(array => {
  // array == ['A', 'B', 'C']
});

// Any value that implements the iterator interface can be used, such as Array, String, Map, Set
mapLimit('abc', 2, str => str.toUpperCase()).then(array => {
  // array == ['A', 'B', 'C']
});

// Iterator object also implements the iterator interface
const arr = ['a', 'b', 'c'];
const iterator = a.entries();
mapLimit(iterator, 2, entry => entry).then(array => {
  // array == [
  //   [0, 'a'],
  //   [1, 'b'],
  //   [2, 'c']
  // ]
});

// If the iterator is omitted, each function in the array is executed
// This will run max two functions at a time
mapLimit([
  () => Promise.resolve(1),
  () => Promise.resolve(2),
  () => 3, // can be sync
  () => Promise.resolve(4),
], 2).then(array => {
  // array == [1, 2, 3, 4]
});