Comparing version 2.0.0 to 2.0.1
19
index.js
'use strict' | ||
var PromiseConstructor; | ||
if (typeof Promise === 'function') { | ||
PromiseConstructor = Promise; | ||
} | ||
module.exports = function (PromiseArgument) { | ||
@@ -36,3 +31,3 @@ var Promise; | ||
fn = size; | ||
size = fn; | ||
size = temp; | ||
} | ||
@@ -58,3 +53,6 @@ if (typeof fn === 'function') { | ||
if (typeof arguments[0] === 'number' || typeof arguments[1] === 'number') { | ||
Promise = PromiseConstructor; | ||
Promise = module.exports.Promise; | ||
if (typeof Promise !== 'function') { | ||
throw new Error('You must provide a Promise polyfill for this library to work in older environments'); | ||
} | ||
return throat(arguments[0], arguments[1]); | ||
@@ -67,2 +65,7 @@ } else { | ||
/* istanbul ignore next */ | ||
if (typeof Promise === 'function') { | ||
module.exports.Promise = Promise; | ||
} | ||
function Delayed(resolve, fn, self, args) { | ||
@@ -72,3 +75,3 @@ this.resolve = resolve | ||
this.self = self || null | ||
this.args = args || null | ||
this.args = args | ||
} |
{ | ||
"name": "throat", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Throttle the parallelism of an asynchronous (promise returning) function / functions", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
'use strict'; | ||
var assert = require('assert'); | ||
var fs = require('fs'); | ||
var test = require('testit'); | ||
var Promise = require('promise'); | ||
var Promise = require('promise/lib/es6-extensions.js'); | ||
var throat = require('../')(Promise); | ||
@@ -18,2 +19,3 @@ | ||
executeJob.isRun = true; | ||
executeJob.args = Array.prototype.slice.call(arguments); | ||
return promise; | ||
@@ -54,5 +56,5 @@ } | ||
var a = job(), b = job(), c = job(); | ||
var resA = lock(a) | ||
var resB = lock(b) | ||
var resC = lock(c) | ||
var resA = lock(a, 123) | ||
var resB = lock(b, 456) | ||
var resC = lock(c, 789) | ||
assert(a.isRun) | ||
@@ -79,2 +81,5 @@ assert(!b.isRun) | ||
assert(c.isRun) | ||
assert.deepEqual(a.args, [123]); | ||
assert.deepEqual(b.args, [456]); | ||
assert.deepEqual(c.args, [789]); | ||
c.complete(sentC) | ||
@@ -190,1 +195,55 @@ return resC | ||
}) | ||
test('throat(fn, n)', function () { | ||
test('throat(fn, 1) acts as a sequential worker', function (done) { | ||
Promise.all([sentA, sentB, sentC].map(throat(worker(1), 1))) | ||
.then(function (res) { | ||
assert(res[0] instanceof Processed && res[0].val.length > 1 && res[0].val[0] === sentA) | ||
assert(res[1] instanceof Processed && res[1].val.length > 1 && res[1].val[0] === sentB) | ||
assert(res[2] instanceof Processed && res[2].val.length > 1 && res[2].val[0] === sentC) | ||
}) | ||
.nodeify(done) | ||
}) | ||
test('throat(fn, 2) works on two inputs in parallel', function (done) { | ||
Promise.all([sentA, sentB, sentC].map(throat(worker(2), 2))) | ||
.then(function (res) { | ||
assert(res[0] instanceof Processed && res[0].val.length > 1 && res[0].val[0] === sentA) | ||
assert(res[1] instanceof Processed && res[1].val.length > 1 && res[1].val[0] === sentB) | ||
assert(res[2] instanceof Processed && res[2].val.length > 1 && res[2].val[0] === sentC) | ||
}) | ||
.nodeify(done) | ||
}) | ||
test('throat(fn, 3) works on three inputs in parallel', function (done) { | ||
Promise.all([sentA, sentB, sentC].map(throat(worker(3), 3))) | ||
.then(function (res) { | ||
assert(res[0] instanceof Processed && res[0].val.length > 1 && res[0].val[0] === sentA) | ||
assert(res[1] instanceof Processed && res[1].val.length > 1 && res[1].val[0] === sentB) | ||
assert(res[2] instanceof Processed && res[2].val.length > 1 && res[2].val[0] === sentC) | ||
}) | ||
.nodeify(done) | ||
}) | ||
}) | ||
test('with native promises', function (done) { | ||
var throat = require('../') | ||
throat.Promise = Promise | ||
Promise.all([sentA, sentB, sentC].map(throat(1, worker(1)))) | ||
.then(function (res) { | ||
assert(res[0] instanceof Processed && res[0].val.length > 1 && res[0].val[0] === sentA) | ||
assert(res[1] instanceof Processed && res[1].val.length > 1 && res[1].val[0] === sentB) | ||
assert(res[2] instanceof Processed && res[2].val.length > 1 && res[2].val[0] === sentC) | ||
}) | ||
.nodeify(done) | ||
}) | ||
test('without native promises', function () { | ||
var throat = require('../') | ||
throat.Promise = null | ||
try { | ||
throat(1) | ||
} catch (ex) { | ||
assert(/provide a Promise polyfill/.test(ex.message)); | ||
return; | ||
} | ||
throw new Error('Expected a failure'); | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15275
348
3