Skip to content

Commit

Permalink
Use native promise.
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor committed Jan 3, 2016
1 parent 67537e9 commit c8d3421
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"before",
"beforeEach",
"after",
"afterEach"
"afterEach",
"Promise"
],

"node": true,
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var arrayRemove = require('./lib/arrayRemove');
var Promise = require('promise');

function PThrottler(defaultConcurrency, types) {
this._defaultConcurrency = typeof defaultConcurrency === 'number' ? defaultConcurrency : 10;
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"version": "1.0.1",
"description": "A promise based throttler capable of limiting execution of parallel tasks",
"main": "index.js",
"dependencies": {
"promise": "^7.1.1"
},
"devDependencies": {
"expect.js": "^0.3.1",
"mocha": "^2.1.0"
Expand Down
32 changes: 13 additions & 19 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var expect = require('expect.js');
var Promise = require('promise');
var PThtroller = require('../');

describe('PThtroller', function () {
Expand Down Expand Up @@ -30,46 +29,42 @@ describe('PThtroller', function () {
expect(promise.then).to.be.a('function');
});

it('should call the function and fulfill the promise accordingly', function (next) {
it('should call the function and fulfill the promise accordingly', function () {
var throttler = new PThtroller();

throttler.enqueue(function () { return 'foo'; })
return throttler.enqueue(function () { return 'foo'; })
.then(function (ret) {
expect(ret).to.equal('foo');

return throttler.enqueue(function () { return Promise.reject(new Error('foo')); });
})
.catch(function (err) {
.then(function () {
throw new Error('Should have failed!');
}, function (err) {
expect(err).to.be.an(Error);
expect(err.message).to.equal('foo');
next();
})
.done();
});
});

it('should work with functions that return values syncronously', function (next) {
it('should work with functions that return values syncronously', function () {
var throttler = new PThtroller();

throttler.enqueue(function () { return 'foo'; })
.then(function (ret) {
expect(ret).to.equal('foo');
next();
})
.done();
});
});

it('should work with functions that throw syncronously', function (next) {
it('should work with functions that throw syncronously', function () {
var throttler = new PThtroller();

throttler.enqueue(function () { throw new Error('bar'); })
return throttler.enqueue(function () { throw new Error('bar'); })
.then(function () {
throw new Error('Should not be called!');
}, function (err) {
expect(err).to.be.an(Error);
expect(err.message).to.be('bar');
next();
})
.done();
});
});

it('should assume the default concurrency when a type is not specified', function (next) {
Expand Down Expand Up @@ -190,12 +185,11 @@ describe('PThtroller', function () {
});
}, 'foo');

setTimeout(function () {
timeout = setTimeout(function () {
throttler.abort().then(function () {
expect(calls).to.eql([1, 2, 3, 4]);
next();
})
.done();
.then(next, next);
}, 25);
});
});
Expand Down

0 comments on commit c8d3421

Please sign in to comment.