Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-parallel-throttle

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-parallel-throttle

Run promises in parallel, but throttled

  • 3.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26K
increased by7.03%
Maintainers
1
Weekly downloads
 
Created
Source

Promise-parallel-throttle

Build Status

Run a array of Promises in parallel. Kinda like Promise.all(), but throttled!

Install

NPM

npm i promise-parallel-throttle -S

Yarn

yarn add promise-parallel-throttle

Usage

import Throttle from 'promise-parallel-throttle';

//Function which should return a Promise
const doReq = async (firstName, lastName) => {
    //Do something async.
    return firstName + " " + lastName;
}

const users = [
    {firstName: "Irene", lastName: "Pullman"},
    {firstName: "Sean",  lastName: "Parr"}
];

//Queue with functions to be run
const queue = users.map(user => () => doReq(user.firstName, user.lastName));

//Default Throttle runs with 5 promises parallel.
const formattedNames = await Throttle.all(queue);

console.log(formattedNames); //['Irene Pullman', 'Sean Parr']

API

Throttle.all

Throttle.all(tasks, options)

Throttle.all is made to behave exactly like Promise.all but instead of all the tasks run parallel it runs a max amount of tasks parallel. All the tasks array parameter is required while the options parameter is optional.

Throttle.sync

Throttle.sync(tasks, options)

Throttle.sync runs all the tasks synchronously. Once again the tasks array is required, the options are optional.

Throttle.raw

Throttle.raw(tasks, options)

The raw method instead of returning the tasks their results will return a Result object. Useful if you wan't more statistics about the execution of your tasks.

Option's Object
ParameterTypeDefaultDefinition
maxInProgressInteger5max amount of parallel threads
failFastBooleanfalsereject after a single error, or keep running
progressCallbackFunctionOptionalcallback with progress reports
nextCheckFunctionOptionalfunction which should return a promise, if the promise resolved true the next task is spawn
Result object / Progress callback

The progressCallback and the Raw will return a Result object with the following properties:

PropertyTypeStart valueDefinition
amountDoneInteger0amount of tasks which are finished
amountStartedInteger0amount of tasks which started
amountResolvedInteger0amount of tasks which successfully resolved
amountRejectedInteger0amount of tasks which returned in an error and are aborted
rejectedIndexesArray[]all the indexes in the tasks array where the promise rejected
resolvedIndexesArray[]all the indexes in the tasks array where the promise resolved
taskResultsArray[]array containing the result of every task
nextCheck

All the methods have a nextCheck method which will be used to verify if a next task is allowed to start.

The default nextCheck look like this;

const defaultNextTaskCheck = (status, tasks) => {
    return new Promise((resolve, reject) => {
        resolve(status.amountStarted < tasks.length);
    });
}

This function will get a status object as parameter which adheres to the object in Result object / Progress callback and also it receives the list of tasks. In the default nextCheck we simply check if the amount of started exceeds the amount to be done, if not we are free to start an other task.

This function can be useful to write your own scheduler based on, for example ram usage/cpu usage. Lets say the tasks you defined use a lot of ram and you don't want to exceed a certain amount. Then you could write logic inside a nextCheck function which resolves after there is enough ram available to start the next task.

If a custom implementation decides to reject the error is propagated and should be handled in the user it's code.

Example

Check out the example's directory, it's heavily documented so it should be easy to follow.

To run the example, at least Node 7.x.x is required, since it supports native async/await.

Simply run the example with npm:

npm run-script names

Or with Yarn:

yarn names

FAQs

Package last updated on 04 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc