es6-promise-pool
Advanced tools
Comparing version 2.3.0 to 2.4.0
/* global define */ | ||
(function (root, factory) { | ||
/* istanbul ignore if */ | ||
if (typeof define === 'function' && define.amd) { | ||
define([], factory) | ||
} else if (typeof exports === 'object') { | ||
} else /* istanbul ignore else */ if (typeof exports === 'object') { | ||
module.exports = factory() | ||
@@ -45,34 +46,44 @@ } else { | ||
var generatorFunctionToProducer = function (gen) { | ||
gen = gen() | ||
return function () { | ||
var res = gen.next() | ||
return res.done ? null : res.value | ||
var isGenerator = function (func) { | ||
return (typeof func.constructor === 'function' && | ||
func.constructor.name === 'GeneratorFunction') | ||
} | ||
var functionToIterator = function (func) { | ||
return { | ||
next: function () { | ||
var promise = func() | ||
return promise ? {value: promise} : {done: true} | ||
} | ||
} | ||
} | ||
var promiseToProducer = function (promise) { | ||
var promiseToIterator = function (promise) { | ||
var called = false | ||
return function () { | ||
if (called) { | ||
return null | ||
return { | ||
next: function () { | ||
if (called) { | ||
return {done: true} | ||
} | ||
called = true | ||
return {value: promise} | ||
} | ||
called = true | ||
return promise | ||
} | ||
} | ||
var toProducer = function (obj, Promise) { | ||
var toIterator = function (obj, Promise) { | ||
var type = typeof obj | ||
if (type === 'function') { | ||
if (obj.constructor && obj.constructor.name === 'GeneratorFunction') { | ||
return generatorFunctionToProducer(obj) | ||
} else { | ||
if (type === 'object') { | ||
if (typeof obj.next === 'function') { | ||
return obj | ||
} | ||
/* istanbul ignore else */ | ||
if (typeof obj.then === 'function') { | ||
return promiseToIterator(obj) | ||
} | ||
} | ||
if (type !== 'object' || typeof obj.then !== 'function') { | ||
obj = Promise.resolve(obj) | ||
if (type === 'function') { | ||
return isGenerator(obj) ? obj() : functionToIterator(obj) | ||
} | ||
return promiseToProducer(obj) | ||
return promiseToIterator(Promise.resolve(obj)) | ||
} | ||
@@ -96,4 +107,4 @@ | ||
this._options.promise = this._options.promise || Promise | ||
this._producer = toProducer(source, this._options.promise) | ||
this._producerDone = false | ||
this._iterator = toIterator(source, this._options.promise) | ||
this._done = false | ||
this._size = 0 | ||
@@ -190,13 +201,12 @@ this._promise = null | ||
PromisePool.prototype._proceed = function () { | ||
if (!this._producerDone) { | ||
var promise | ||
while (this._size < this._concurrency && (promise = this._producer())) { | ||
if (!this._done) { | ||
var result = null | ||
while (this._size < this._concurrency && | ||
!(result = this._iterator.next()).done) { | ||
this._size++ | ||
this._trackPromise(promise) | ||
this._trackPromise(result.value) | ||
} | ||
if (!promise) { | ||
this._producerDone = true | ||
} | ||
this._done = (result === null || !!result.done) | ||
} | ||
if (this._producerDone && this._size === 0) { | ||
if (this._done && this._size === 0) { | ||
this._settle() | ||
@@ -203,0 +213,0 @@ } |
{ | ||
"name": "es6-promise-pool", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Runs Promises in a pool that limits their maximum concurrency.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -9,3 +9,3 @@ # ES6 Promise Pool | ||
An ECMAScript 6 `Promise` is a great way of handling asynchronous operations. | ||
An ECMAScript 2015 `Promise` is a great way to handle asynchronous operations. | ||
The `Promise.all` function provides an easy interface to let a bunch of promises | ||
@@ -24,4 +24,4 @@ settle concurrently. | ||
The promises can be created in a just-in-time fashion. You essentially pass a | ||
function that produces a new promise every time it is called. On modern | ||
platforms, you can also use ES6 generator functions for this. | ||
function that produces a new promise every time it is called. Alternatively, you | ||
can pass an ES2015 iterator, meaning you can also use generator functions. | ||
@@ -122,8 +122,10 @@ ## Compatibility | ||
### Generator | ||
### Iterator | ||
We can achieve the same result with ECMAScript 6 generator functions. | ||
We can achieve the same result with ECMAScript 2015 iterators. Since ES2015 | ||
generator functions return such an iterator, we can make the example above a lot | ||
prettier: | ||
```js | ||
const promiseProducer = function * () { | ||
const generatePromises = function * () { | ||
for (let count = 1; count <= 5; count++) { | ||
@@ -134,3 +136,4 @@ yield delayValue(count, 1000) | ||
const pool = new PromisePool(promiseProducer, 3) | ||
const promiseIterator = generatePromises() | ||
const pool = new PromisePool(promiseIterator, 3) | ||
@@ -141,2 +144,6 @@ pool.start() | ||
It's also possible to pass a generator function directly. In that case, it will | ||
be invoked with no arguments and the resulting iterator will be used. This | ||
feature will however be removed in version 3. | ||
## Events | ||
@@ -143,0 +150,0 @@ |
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
15675
194
214