throat 3.0.0

Throttle the parallelism of an asynchronous (promise returning) function / functions

Homepage: https://github.com/ForbesLindesay/throat

Platform: npm

Language: JavaScript

License: MIT

Keywords: promise, aplus, then, throttle, concurrency, parallelism, limit

View on registry: https://www.npmjs.com/package/throat

Direct download link: https://registry.npmjs.org/throat/-/throat-3.0.0.tgz

Install: npm install [email protected]


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.

Build Status Coverage Status Dependency Status NPM version

Sauce Test Status

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:

// with polyfill or in iojs
require('promise/polyfill')
var throat = require('throat')(2)
// alternatively provide your own promise implementation
var throat = require('throat')(require('promise'))(2)

var resA = throat(function () {
  //async stuff
  return promise
})
var resA = throat(function () {
  //async stuff
  return promise
})
var resA = throat(function () {
  //async stuff
  return promise
})
var resA = throat(function () {
  //async stuff
  return promise
})
var resA = throat(function () {
  //async stuff
  return 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:

// with polyfill or in iojs
require('promise/polyfill')
var throat = require('throat')
// alternatively provide your own promise implementation
var throat = require('throat')(require('promise'))

var input = ['fileA.txt', 'fileB.txt', 'fileC.txt', 'fileD.txt']
var data = Promise.all(input.map(throat(2, function (fileName) {
  return readFile(fileName)
})))

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

License

MIT

Sustain

Releases

3.0.0 June 01, 2016
2.0.2 February 16, 2015
2.0.1 February 14, 2015
2.0.0 February 14, 2015
1.0.0 July 27, 2013
0.0.0 December 23, 2012

Project Statistics

SourceRank 18
Dependencies 0
Dependent projects 73
Dependent repositories 1.59K
Total releases 6
Latest release
First release
Stars 178
Forks 9
Watchers 5
Contributors 1
Repo Size: 15.6 KB

Top Contributors See all

Forbes Lindesay

Something wrong with this page? Make a suggestion

Export .ABOUT file for this library

Last synced: 2016-10-13 10:42:39 UTC

Login to resync this project