-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"test": "node ../node_modules/mocha/bin/mocha" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
||
|
||
}); | ||
}); |