robert2206 8ad867f7a5 commit inicial | 8 years ago | |
---|---|---|
.. | ||
test | 8 years ago | |
.npmignore | 8 years ago | |
.travis.yml | 8 years ago | |
LICENSE | 8 years ago | |
README.md | 8 years ago | |
index.js | 8 years ago | |
package.json | 8 years ago |
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.
npm install throat
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
})
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.
MIT