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

run-in-batch

v1.0.7

Published

Run your task like api calls in batch

Downloads

6

Readme

run-in-batch

Run your task like api calls in batch

features

  • Run either parallely or serially
  • Set number of tasks in one batch
  • Set timeout for each task
  • Callback function for completion of each task
  • Callback function post completion of each batch

install

To install the latest version:

npm install --save run-in-batch

examples

To perform one operation per batch, use 'onBatchRun`:

const run = require("run-in-batch");

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
const options = {
  batchSize: 3,
  onBatchRun: (value) => Promise.resolve(value)
};
const results = await run(arr, options);

To perform one operation for each item in the batch use 'onTaskRun`, while each batch tasks running concurrently:

const run = require("run-in-batch");

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
const options = {
  batchSize: 3,
  onTaskRun: (value) => Promise.resolve(value)
};
const results = await run(arr, options);

To perform one operation for each item in the batch use 'onTaskRun`, while each batch tasks running sequentially:

const run = require("run-in-batch");

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
const options = {
  runType: "series",
  batchSize: 3,
  onTaskRun: (value) => Promise.resolve(value)
};
const results = await run(arr, options);

To use the results, after each batch completes:

const run = require("run-in-batch");

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
const options = {
  batchSize: 3,
  onTaskRun: (value) => Promise.resolve(value),
  onBatchComplete: (tasks, options, results) => {
    console.log(`Completed processing of ${results.length} tasks`);
  }
};
const results = await run(arr, options);

// Completed processing of 3 tasks
// Completed processing of 6 tasks
// Completed processing of 9 tasks
// Completed processing of 12 tasks
// Completed processing of 13 tasks

To set timeout for each task's execution:

const run = require("run-in-batch");

const userIds = [
  { name: "Alex", time: 150 },
  { name: "Bob", time: 250 },
  { name: "Carol", time: 200 },
  { name: "Dennis", time: 50 },
  { name: "Eric", time: 100 }
];

const options = {
  batchSize: 5,
  onTaskRun: (user) =>
    new Promise((resolve) => {
      setTimeout(() => resolve(user.name), user.time);
    }),
  taskTimeout: 225
};
const results = await run(userIds, options);
// "Alex", "timeout", "Carol", "Dennis", "Eric"

options

Execute task in a batch parallely or sequentially

runType (Default: parallel)

  • parallel
  • series

Set number of tasks in one batch

batchSize (Optional)

  • If not specified, all tasks will execute concurrently
  • If specified, tasks are chunked by batchSize

Set timeout for each task

taskTimeout (Optional)

  • If not specified, all tasks will run indefinately
  • If specified, each task races against this timeout. Value specified is considered as milliseconds.

If timeout happens, task would return:

taskTimeoutVal

Default value: "timeout"

Callback function for completion of each task

onTaskRun (Optional)

  • If not specified, empty array is returned
  • If specified, this method is run for each task

Callback function for completion of each batch

onBatchRun (Optional)

  • If not specified, you can opt to use onTaskRun for each item individually.
  • If specified, this method is run for each batch instead of processing each item individually.

Callback function post completion of each batch

onBatchComplete (Optional)

  • If not specified, no error is thrown
  • If specified, this method is run post each batch completion

note

The task array passed is spliced directly to optmise memory usage. If you intend to use it later, please clone it before calling run.

Also, ES generators are used to fetch batches.