Skip to content

Commit

Permalink
moving to Promise.reject instead of throw and updating unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brozeph committed May 11, 2016
1 parent bfa3a52 commit 83d5650
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.1.4 / 2016.05.11

* Adjusted interface to `#series` to `Promise.reject` rather than throw

# v0.1.3 / 2016.05.05

* Introduced new method `series` to assist with running a series of Promise closures in order
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "renege",
"version": "0.1.3",
"version": "0.1.4",
"description": "Convenience methods for turning a function with a callback argument into a native promise",
"main": "promise.js",
"scripts": {
Expand Down
22 changes: 18 additions & 4 deletions promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,31 @@ module.exports = (function (self) {
* @returns {Promise} A Promise that resolves once each item within the input list is resolved
**/
self.series = function (list) {
if (!list) {
throw new Error('list of functions that return a Promise is required');
var err;

if (!list || !Array.isArray(list)) {
return Promise.reject(
new Error('list of functions that return a Promise is required'));
}

// validate that each item in the input list is a function
list.forEach(function (closure, index) {
list.some(function (closure, index) {
if (typeof closure !== 'function') {
throw new Error(['item at index', index, 'is not a function'].join(' '));
err = new Error([
'item at index',
index,
'is not a function'].join(' '));

return true;
}

return false;
});

if (err) {
return Promise.reject(err);
}

// if there are no items in the list, simply return a Promise
if (!list.length) {
return Promise.resolve();
Expand Down
44 changes: 28 additions & 16 deletions test/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('renege', function () {
should.exist(renege);
should.exist(renege.series);

renege.series.should.throw(/required/);
renege.series().should.be.rejectedWith(/required/);
});

it('should require that each item in the list is a function', function () {
Expand All @@ -82,29 +82,41 @@ describe('renege', function () {
},
Promise.resolve(2)];

(function () {
return renege.series(badList);
}).should.throw(/index 1/);
return renege.series(badList).should.be.rejectedWith(/index 1/);
});

it('should resolve immediately if list is empty', function () {
renege.series([]).should.eventually.resolve;
});

it('should reject immediately when a promise within the array rejects', function () {
var list = [
function () {
return renege.create(mockSuccessMethod, 'test 1');
},
function () {
return renege.create(mockErrorMethod, 'test error');
},
function () {
return renege.create(mockSuccessMethod, 'test 2');
}
];
var
counter = 0,
list = [
function () {
return renege.create(function (callback) {
counter++;

return setImmediate(callback);
});
},
function () {
return renege.create(mockErrorMethod, 'test error');
},
function () {
return renege.create(function (callback) {
counter++;

return setImmediate(callback);
});
}
];

renege.series(list).should.be.rejectedWith(/test error/);
renege.series(list)
.should.be.rejectedWith(/test error/)
.notify(function () {
counter.should.equal(1);
});
});

it('should resolve in order after all Promises resolve', function () {
Expand Down

0 comments on commit 83d5650

Please sign in to comment.