Skip to content

Commit

Permalink
rename to directly
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 8, 2015
1 parent f5519aa commit dd06929
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# promise-throttle
# directly

## Like Promise.all, only less so

This module could more descriptively be named Promise.allButNotAllAtOnce. It takes an array of functions which return a promise and returns a promise which resolves once all the promises resolved by the promises have resolved, or otherwise rejects. The difference is that a maximum of `n` promises are created at any one time. This is useful for rate-limiting asynchronous calls which return promises (e.g. `fetch`, `mongoose`...);
This module could more descriptively be named Promise.allButNotAllAtOnce. It takes an array of functions, each of which return a promise, and returns a promise which resolves once all those promises have resolved, or otherwise rejects... very similar to `Promise.all`. The difference is that a maximum of `n` promises are created at any one time. This is useful for rate-limiting asynchronous calls (e.g. `fetch`, `mongoose`...);

## About the name
In Devon people will often promise to do things 'directly', meaning they'll do it when they're good and ready

## Usage

```
var PromiseThrottle = require('promise-throttle`);
var Directly = require('directly`);
var urls = []; // a big array of urls
var fetchers = urls.map(function (url) {
return function () {
return fetch(url);
}
});
var throttledRequests = new PromiseThrottle(10, fetchers)
var throttledRequests = new Directly(10, fetchers)
throttledRequests
.run()
Expand All @@ -25,13 +28,14 @@ throttledRequests
```

Can also be called as a function (which implicitly calls the `.run()` method internally)
Can also be called as a function (which calls the `.run()` method internally)

```
var promiseThrottle = require('promise-throttle');
promiseThrottle(10, fetchers)
var directly = require('directly');
directly(10, fetchers)
.then(function (results) {
// handle exactly as if it was a Promise.all()
});
```

*Based on an idea originally developed [at FT](https://github.com/Financial-Times/next-user-preferences-api-v2/blob/master/lib/promise-throttle.js)*
*Based on an idea originally developed [at FT](https://github.com/Financial-Times/next-user-preferences-api-v2/blob/master/lib/directly.js)*
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "promise-throttle",
"main": "promise-throttle.js",
"name": "directly",
"main": "directly.js",
"version": "0.0.0",
"homepage": "https://github.com/wheresrhys/promise-throttle",
"homepage": "https://github.com/wheresrhys/directly",
"authors": [
"Rhys Evans <wheresrhys@gmail.com>"
],
Expand Down
20 changes: 10 additions & 10 deletions promise-throttle.js → directly.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

require('es6-promise').polyfill();

var PromiseThrottle = function (throttleLimit, funcs) {
if (!(this instanceof PromiseThrottle)) {
return new PromiseThrottle(throttleLimit, funcs).run();
var Directly = function (concurrence, funcs) {
if (!(this instanceof Directly)) {
return new Directly(concurrence, funcs).run();
}
this.results = [];
this.throttleLimit = throttleLimit;
this.concurrence = concurrence;
this.funcs = funcs;
this.counter = 0;
};

PromiseThrottle.prototype.run = function () {
if (this.funcs.length <= this.throttleLimit) {
Directly.prototype.run = function () {
if (this.funcs.length <= this.concurrence) {
return Promise.all(this.funcs.map(function (func) {
return func();
}));
Expand All @@ -24,7 +24,7 @@ PromiseThrottle.prototype.run = function () {
this.reject = reject;
}.bind(this));

var i = this.throttleLimit;
var i = this.concurrence;

while (i--) {
this.executeOne();
Expand All @@ -34,7 +34,7 @@ PromiseThrottle.prototype.run = function () {
return this.promise;
};

PromiseThrottle.prototype.executeOne = function () {
Directly.prototype.executeOne = function () {
var promise = this.funcs.shift()();

this.results.push(promise);
Expand All @@ -46,7 +46,7 @@ PromiseThrottle.prototype.executeOne = function () {
this.competitors.push(promise);
};

PromiseThrottle.prototype.startRace = function () {
Directly.prototype.startRace = function () {
Promise.race(this.competitors)
.then(function (index) {
if (!this.funcs.length) {
Expand All @@ -67,4 +67,4 @@ PromiseThrottle.prototype.startRace = function () {
};


module.exports = PromiseThrottle;
module.exports = Directly;
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "promise-throttle",
"version": "1.0.0",
"name": "directly",
"version": "1.0.1",
"description": "Like Promise.all, only less so",
"main": "promise-throttle.js",
"main": "directly.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/wheresrhys/promise-throttle.git"
"url": "https://github.com/wheresrhys/directly.git"
},
"keywords": [
"promise",
Expand All @@ -20,9 +20,9 @@
"author": "Rhys Evans",
"license": "MIT",
"bugs": {
"url": "https://github.com/wheresrhys/promise-throttle/issues"
"url": "https://github.com/wheresrhys/directly/issues"
},
"homepage": "https://github.com/wheresrhys/promise-throttle",
"homepage": "https://github.com/wheresrhys/directly",
"devDependencies": {
"chai": "^3.2.0",
"mocha": "^2.2.5"
Expand Down
22 changes: 11 additions & 11 deletions test/promise-throttle.test.js → test/directly.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var expect = require('chai').expect;
var PromiseThrottle = require('../promise-throttle');
var Directly = require('../directly');

var setupPromises = function (n) {

Expand Down Expand Up @@ -49,7 +49,7 @@ describe('concordance with Promise.all', function () {
it('should resolve if they all resolve', function (done) {
var promises = setupPromises(2);

new PromiseThrottle(3, promises.functions).run()
new Directly(3, promises.functions).run()
.then(function (res) {
expect(res).to.eql([0, 1]);
done();
Expand All @@ -64,7 +64,7 @@ describe('concordance with Promise.all', function () {
it('should reject if any of them reject', function (done) {
var promises = setupPromises(2);

new PromiseThrottle(3, promises.functions).run()
new Directly(3, promises.functions).run()
.catch(function (res) {
expect(res).to.equal('err1');
done();
Expand All @@ -79,7 +79,7 @@ describe('concordance with Promise.all', function () {
it('should resolve if they all resolve', function (done) {
var promises = setupPromises(3);

new PromiseThrottle(3, promises.functions).run()
new Directly(3, promises.functions).run()
.then(function (res) {
expect(res).to.eql([0, 1, 2]);
done();
Expand All @@ -94,7 +94,7 @@ describe('concordance with Promise.all', function () {
it('should reject if any of them reject', function (done) {
var promises = setupPromises(3);

new PromiseThrottle(3, promises.functions).run()
new Directly(3, promises.functions).run()
.catch(function (res) {
expect(res).to.equal('err1');
done();
Expand All @@ -108,7 +108,7 @@ describe('concordance with Promise.all', function () {
it('should resolve if they all resolve', function (done) {
var promises = setupPromises(4);

new PromiseThrottle(3, promises.functions).run()
new Directly(3, promises.functions).run()
.then(function (res) {
expect(res).to.eql([0, 1, 2, 3]);
done();
Expand All @@ -123,7 +123,7 @@ describe('concordance with Promise.all', function () {
it('should reject if any of them reject', function (done) {
var promises = setupPromises(4);

new PromiseThrottle(3, promises.functions).run()
new Directly(3, promises.functions).run()
.catch(function (res) {
expect(res).to.equal('err1');
done();
Expand All @@ -135,7 +135,7 @@ describe('concordance with Promise.all', function () {
it('should preserve promise order in the results', function () {
var promises = setupPromises(4);

new PromiseThrottle(3, promises.functions).run()
new Directly(3, promises.functions).run()
.then(function (res) {
expect(res).to.eql([1,2,3,4]);
done();
Expand All @@ -158,7 +158,7 @@ describe('throttling', function () {
spyVal = true;
});

new PromiseThrottle(3, promises.functions).run()
new Directly(3, promises.functions).run()

expect(spyVal).to.be.false;
});
Expand All @@ -179,7 +179,7 @@ describe('throttling', function () {
return func4();
}

new PromiseThrottle(3, promises.functions).run()
new Directly(3, promises.functions).run()

expect(spyVals).to.eql([]);

Expand Down Expand Up @@ -208,7 +208,7 @@ describe('functional calling', function () {
it('should be callable as a function', function (done) {
var promises = setupPromises(3);

PromiseThrottle(3, promises.functions)
Directly(3, promises.functions)
.then(function (res) {
expect(res).to.eql([0, 1, 2]);
done();
Expand Down

0 comments on commit dd06929

Please sign in to comment.