Socket
Socket
Sign inDemoInstall

throat

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

throat - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

test/browser.js

86

index.js
'use strict'
var Promise = require('promise')
var PromiseConstructor;
if (typeof Promise === 'function') {
PromiseConstructor = Promise;
}
module.exports = throat
function throat(size, fn) {
var queue = []
function run(fn, self, args) {
if (size) {
size--
var result = new Promise(function (resolve) {
resolve(fn.apply(self, args))
})
result.done(release, release)
return result
} else {
return new Promise(function (resolve) {
queue.push(new Delayed(resolve, fn, self, args))
})
module.exports = function (PromiseArgument) {
var Promise;
function throat(size, fn) {
var queue = []
function run(fn, self, args) {
if (size) {
size--
var result = new Promise(function (resolve) {
resolve(fn.apply(self, args))
})
result.done(release, release)
return result
} else {
return new Promise(function (resolve) {
queue.push(new Delayed(resolve, fn, self, args))
})
}
}
}
function release() {
size++
if (queue.length) {
var next = queue.shift()
next.resolve(run(next.fn, next.self, next.args))
function release() {
size++
if (queue.length) {
var next = queue.shift()
next.resolve(run(next.fn, next.self, next.args))
}
}
}
if (typeof fn === 'function') {
return function () {
var args = arguments
return run(fn, this, arguments)
if (typeof size === 'function' && typeof fn === 'number') {
var temp = fn;
fn = size;
size = fn;
}
} else {
return function (fn) {
return run(fn, this, Array.prototype.slice.call(arguments, 1))
if (typeof fn === 'function') {
return function () {
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
return run(fn, this, args)
}
} else {
return function (fn) {
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
return run(fn, this, args)
}
}
}
if (typeof arguments[0] === 'number' || typeof arguments[1] === 'number') {
Promise = PromiseConstructor;
return throat(arguments[0], arguments[1]);
} else {
Promise = PromiseArgument;
return throat;
}
}

@@ -46,2 +70,2 @@

this.args = args || null
}
}
{
"name": "throat",
"version": "1.0.0",
"version": "2.0.0",
"description": "Throttle the parallelism of an asynchronous (promise returning) function / functions",

@@ -14,10 +14,15 @@ "keywords": [

],
"dependencies": {
"promise": "~3.2.0"
},
"dependencies": {},
"devDependencies": {
"mocha": "*"
"coveralls": "^2.11.2",
"istanbul": "^0.3.5",
"promise": "^6.1.0",
"sauce-test": "^1.0.0",
"test-result": "^2.0.0",
"testit": "^2.0.2"
},
"scripts": {
"test": "mocha -R spec"
"test": "node test/index.js && node test/browser.js",
"coverage": "istanbul cover test/index.js",
"coveralls": "npm run coverage && cat ./coverage/lcov.info | coveralls"
},

@@ -24,0 +29,0 @@ "repository": {

@@ -5,6 +5,9 @@ # throat

[![Build Status](https://travis-ci.org/ForbesLindesay/throat.png?branch=master)](https://travis-ci.org/ForbesLindesay/throat)
[![Dependency Status](https://gemnasium.com/ForbesLindesay/throat.png)](https://gemnasium.com/ForbesLindesay/throat)
[![NPM version](https://badge.fury.io/js/throat.png)](http://badge.fury.io/js/throat)
[![Build Status](https://img.shields.io/travis/ForbesLindesay/throat/master.svg)](https://travis-ci.org/ForbesLindesay/throat)
[![Coverage Status](https://img.shields.io/coveralls/ForbesLindesay/throat/master.svg?style=flat)](https://coveralls.io/r/ForbesLindesay/throat?branch=master)
[![Dependency Status](https://img.shields.io/gemnasium/ForbesLindesay/throat.svg)](https://gemnasium.com/ForbesLindesay/throat)
[![NPM version](https://img.shields.io/npm/v/throat.svg)](http://badge.fury.io/js/throat)
[![Sauce Test Status](https://saucelabs.com/browser-matrix/throat.svg)](https://saucelabs.com/u/throat)
## Installation

@@ -23,3 +26,7 @@

```js
// 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)

@@ -53,2 +60,8 @@ var resA = throat(function () {

```js
// 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']

@@ -64,2 +77,2 @@ var data = Promise.all(input.map(throat(2, function (fileName) {

MIT
MIT

@@ -0,4 +1,7 @@

'use strict';
var assert = require('assert');
var test = require('testit');
var Promise = require('promise');
var assert = require('assert');
var throat = require('../');
var throat = require('../')(Promise);

@@ -46,4 +49,4 @@ var sentA = {}, sentB = {}, sentC = {}

describe('throat(n)', function () {
it('throat(1) acts as a lock', function (done) {
test('throat(n)', function () {
test('throat(1) acts as a lock', function (done) {
var lock = throat(1)

@@ -83,3 +86,3 @@ var a = job(), b = job(), c = job();

})
it('throat(2) lets two processes acquire the same lock', function (done) {
test('throat(2) lets two processes acquire the same lock', function (done) {
var lock = throat(2)

@@ -119,3 +122,3 @@ var a = job(), b = job(), c = job();

})
it('throat(3) lets three processes acquire the same lock', function (done) {
test('throat(3) lets three processes acquire the same lock', function (done) {
var lock = throat(3)

@@ -158,4 +161,4 @@ var a = job(), b = job(), c = job();

describe('throat(n, fn)', function () {
it('throat(1, fn) acts as a sequential worker', function (done) {
test('throat(n, fn)', function () {
test('throat(1, fn) acts as a sequential worker', function (done) {
Promise.all([sentA, sentB, sentC].map(throat(1, worker(1))))

@@ -169,3 +172,3 @@ .then(function (res) {

})
it('throat(2, fn) works on two inputs in parallel', function (done) {
test('throat(2, fn) works on two inputs in parallel', function (done) {
Promise.all([sentA, sentB, sentC].map(throat(2, worker(2))))

@@ -179,3 +182,3 @@ .then(function (res) {

})
it('throat(3, fn) works on three inputs in parallel', function (done) {
test('throat(3, fn) works on three inputs in parallel', function (done) {
Promise.all([sentA, sentB, sentC].map(throat(3, worker(3))))

@@ -189,2 +192,2 @@ .then(function (res) {

})
})
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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