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

process-batches

v1.0.1

Published

Asynchronously process series in batches

Downloads

5

Readme

process-batches

Asynchronously process series in batches.

This module allows you to process series of jobs in chunks of predefined size. It can process them in parallel.

Build Status

Example

var processBatches = require('process-batches');
var BATCH_SIZE = 100;

processBatches(i => {
  var offset = i * BATCH_SIZE;
  var limit = BATCH_SIZE;
  return db.getUsers(offset, limit);
}, {
  concurrency: 5
}, users => {
  console.log(`sending email to ${users.length} users`);
  return Promise.all(users.map(user => {
    return mailSvc.sendEmail(user.email);
  }));
})
  .then(() => {
    console.log('done sending emails');
  });

API

processBatches

processBatches(fetchFn, [options], processFn)

Returns a promise that is fulfilled when all the batches are processed.

fetchFn

Function(number index) -> Promise<Array>|Array

Function that takes one parameters, the index of the batch. It is used to fetch the batches. It can either return an array or a promise for an array. To signal the end, either return null or an empty array.

options

This parameter is optional.

Object {
  number concurrency = 1
}

Configuration for processing. size determines the batch sizes. Use concurrency to specify how many batches should be processed in parallel. Alternatively you can also just pass the batch size as a number.

processFn

Function(Array batch, number index) processFn -> Promise

The function that will process the batch. It is passed the current batch and index of the batch. If you return a promise, the module will wait until the promise is fulfilled before starting the next batch.

processBatches.fromArray

Same as processBatches but accepts an array as the first parameter. Batches will be constructed from this array. The options object accepts a size parameter which controls the size of the array chunks to be processed. The options parameter can also be passed as a number as a shorthand for the size.

Example:

processBatches.fromArray([1, 2, 3, 4, 5, 6, 7], 3, batch => {
  console.log(`this batch: [${batch.join(', ')}]!`);
});
// prints:
//   this batch: [1, 2, 3]!
//   this batch: [4, 5, 6]!
//   this batch: [7]!