throat
Throttle the parallelism of an asynchronous (promise returning) function / functions
Last updated 5 months ago by forbeslindesay .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install throat 
SYNC missed versions from official npm registry.

throat

Throttle the parallelism of an asynchronous, promise returning, function / functions. This has special utility when you set the concurrency to 1. That way you get a mutually exclusive lock.

Professionally supported throat is now available

Build Status Coverage Status Dependency Status NPM version Greenkeeper badge

Installation

npm install throat

API

throat(concurrency)

This returns a function that acts a bit like a lock (exactly as a lock if concurrency is 1).

Example, only 2 of the following functions will execute at any one time:

const throat = require('throat')(2);
// alternatively provide your own promise implementation
const throat = require('throat')(require('promise'))(2);
const promise = Promise.resolve();

const resA = throat(() => /* async stuff... */ promise);
const resB = throat(() => /* async stuff... */ promise);
const resC = throat(() => /* async stuff... */ promise);
const resD = throat(() => /* async stuff... */ promise);
const resE = throat(() => /* async stuff... */ promise);

throat(concurrency, worker)

This returns a function that is an exact copy of worker except that it will only execute up to concurrency times in parallel before further requests are queued:

const throat = require('throat');
// alternatively provide your own promise implementation
const throat = require('throat')(require('promise'));

const input = ['fileA.txt', 'fileB.txt', 'fileC.txt', 'fileD.txt'];
const data = Promise.all(input.map(throat(2, fileName => readFile(fileName))));

Only 2 files will be read at a time, sometimes limiting parallelism in this way can improve scalability.

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

License

MIT

Current Tags

  • 5.0.0                                ...           latest (5 months ago)

11 Versions

  • 5.0.0                                ...           5 months ago
  • 4.1.0                                ...           2 years ago
  • 4.0.0                                ...           2 years ago
  • 3.2.0                                ...           2 years ago
  • 3.1.0                                ...           2 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.0                                ...           6 years ago
  • 0.0.0                                ...           7 years ago
Maintainers (1)
Downloads
Today 3,062
This Week 34,996
This Month 84,985
Last Day 8,180
Last Week 41,899
Last Month 167,542
Dependencies (0)
None
Dev Dependencies (9)
Dependents (200)

Copyright 2014 - 2016 © taobao.org |