Skip to content

Commit

Permalink
added throttle-promises
Browse files Browse the repository at this point in the history
  • Loading branch information
kolodny committed Jun 12, 2015
1 parent c47fa8e commit 34dc2ca
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
49 changes: 49 additions & 0 deletions throttle-promises/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Assume we have an array of functions that start an operation that does something async:

```js
var nextValue = 0;
var asyncFactory = function() {
var resolveWith = nextValue++;
return new Promise(function(resolve) {
setTimeout(function() {
resolve(resolveWith + '!');
}, Math.random() * 100);
});
};
```

Here's how we would use this without throttling:

```js
Promise.all([ asyncFactory(), asyncFactory(), asyncFactory(), asyncFactory() ]).then(function(results) {
console.log(results); // ['0!', '1!', '2!', '3!'];
});
```

Let's say we need to throttle this to only have a certain amount of promises running at a time


Here's the basic usage of the file that you'll be creating:

```js
var throttlePromises = require('./') // <- this is the file you make;

var nextValue = 0;
var asyncFactory = function() {
var resolveWith = nextValue++;
return new Promise(function(resolve) {
setTimeout(function() {
resolve(resolveWith + '!');
}, Math.random() * 100);
});
};

var arr = [];
for (var i = 0; i < 100; i++) {
arr.push(asyncFactory); // push the factory but don't instantiated since that would start it now
}

// this is the solution function you'll write
throttlePromises(5, arr).then(function(results) {
console.log('only 5 promises were ever executing in parallel');
});
6 changes: 6 additions & 0 deletions throttle-promises/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"scripts": {
"test": "node ../node_modules/mocha/bin/mocha"
}
}
43 changes: 43 additions & 0 deletions throttle-promises/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var assert = require('assert');
var throttlePromises = require('./');

describe('throttle-promises', function() {
it("doesn't run more than `limit` promises in parallel", function(done) {
var passed = true;
var limit = 5;
var currentlyExecuting = 0;
var maxCurrentlyExecuting = 0;
var nextValue = 0;
var asyncFactory = function() {
return new Promise(function(resolve) {
var resolveWith = nextValue++;
currentlyExecuting++;
maxCurrentlyExecuting = Math.max(maxCurrentlyExecuting, currentlyExecuting);
if (currentlyExecuting > limit) {
passed = false;
}
setTimeout(function() {
resolve(resolveWith + '!');
currentlyExecuting--;
}, Math.random() * 100);
});
};

var arr = [];
for (var i = 0; i < 100; i++) {
arr.push(asyncFactory);
}

throttlePromises(5, arr).then(function(results) {
var expectedResults = Array(101).join('.').split('').map(function(dot, index) {
return index + '!';
});
assert(passed, 'more than ' + limit + ' promises ran in parallel');
assert.equal(maxCurrentlyExecuting, limit);
assert.deepEqual(results, expectedResults);
done();
});


});
});

0 comments on commit 34dc2ca

Please sign in to comment.