es6-promise-pool
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "es6-promise-pool", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Runs Promises in a pool that limits their maximum concurrency.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -42,23 +42,29 @@ (function(global) { | ||
var size = 0; | ||
var consumed = false; | ||
var poolPromise = new Promise(function(resolve, reject) { | ||
var failed = false; | ||
var proceed = function() { | ||
var promise; | ||
while (size < concurrency && (promise = producer()) !== null) { | ||
promise.then(function(result) { | ||
size--; | ||
if (!failed) { | ||
onResolve(poolPromise, promise, result); | ||
proceed(); | ||
} | ||
}, function(err) { | ||
if (!failed) { | ||
failed = true; | ||
onReject(poolPromise, promise, err); | ||
reject(err); | ||
} | ||
}); | ||
size++; | ||
if (!consumed) { | ||
var promise; | ||
while (size < concurrency && !!(promise = producer())) { | ||
promise.then(function(result) { | ||
size--; | ||
if (!failed) { | ||
onResolve(poolPromise, promise, result); | ||
proceed(); | ||
} | ||
}, function(err) { | ||
if (!failed) { | ||
failed = true; | ||
onReject(poolPromise, promise, err); | ||
reject(err); | ||
} | ||
}); | ||
size++; | ||
} | ||
if (!promise) { | ||
consumed = true; | ||
} | ||
} | ||
if (size === 0) { | ||
if (consumed && size === 0) { | ||
resolve(); | ||
@@ -65,0 +71,0 @@ } |
@@ -47,8 +47,21 @@ # Promise Pool [![Build Status](https://travis-ci.org/timdp/es6-promise-pool.svg?branch=master)](https://travis-ci.org/timdp/es6-promise-pool) | ||
var promiseProducer = function() { | ||
// There's a 10% chance that we return null, indicating that there are no | ||
// more promises left to process. | ||
if (Math.floor(Math.random() * 10) === 0) { | ||
console.log('No more promises in pool'); | ||
return null; | ||
} | ||
// If we didn't return null, we pass a new promise to the pool. | ||
var promisedValue = new Date().getTime(); | ||
console.log('Creating new promise for ' + promisedValue); | ||
return new Promise(function(resolve, reject) { | ||
setTimeout(resolve, 1000); | ||
setTimeout(function() { | ||
console.log('Resolving promise: ' + promisedValue); | ||
// See below for instructions on how to retrieve the value. | ||
resolve(promisedValue); | ||
}, 1000); | ||
}); | ||
}; | ||
// The number of Promises to process simultaneously. | ||
// The number of promises to process simultaneously. | ||
var concurrency = 3; | ||
@@ -64,3 +77,3 @@ | ||
}, function(error) { | ||
console.log('Some promise rejected: + error.message); | ||
console.log('Some promise rejected: ' + error.message); | ||
}); | ||
@@ -76,3 +89,3 @@ ``` | ||
```js | ||
var delayValue = function(time) { | ||
var delayValue = function(value, time) { | ||
return new Promise(function(resolve, reject) { | ||
@@ -102,3 +115,2 @@ console.log('Resolving ' + value + ' in ' + time + ' ms'); | ||
} else { | ||
// A value of null indicates that there aren't any promises remaining. | ||
return null; | ||
@@ -133,3 +145,3 @@ } | ||
The `options` object lets us provide additional callback function to listen for | ||
The `options` object lets us provide additional callback functions to listen for | ||
promise progress. | ||
@@ -158,2 +170,3 @@ | ||
- [Async.js](https://github.com/caolan/async) | ||
- [Promise Pool](https://github.com/vilic/promise-pool) | ||
@@ -160,0 +173,0 @@ - [qlimit](https://www.npmjs.com/package/qlimit) |
@@ -142,2 +142,21 @@ (function(global) { | ||
it('should not call the producer after terminating it', function() { | ||
var maxCnt = 0; | ||
var cnt = 0; | ||
var poolPromise = promisePool(function() { | ||
if (cnt++ === 3) { | ||
return null; | ||
} else { | ||
maxCnt = Math.max(maxCnt, cnt); | ||
return new Promise(function(resolve, reject) { | ||
setTimeout(resolve, 10); | ||
}); | ||
} | ||
}, 1); | ||
var cntPromise = poolPromise.then(function() { | ||
return maxCnt; | ||
}); | ||
return expect(cntPromise).to.eventually.equal(3); | ||
}); | ||
it('should call onresolve', function() { | ||
@@ -144,0 +163,0 @@ var arg = null; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21770
408
194