es6-promise-series
Advanced tools
Comparing version 0.0.2 to 0.1.0
54
index.js
"use strict"; | ||
module.exports = function(Promise) | ||
{ | ||
Promise.series = function(promises) { | ||
var promises = promises.slice(); | ||
var results = []; | ||
var first = true; | ||
module.exports = function(Promise) { | ||
Promise.series = function (promises, concurrent) { | ||
var results = null; | ||
promises = promises.slice(); | ||
concurrent = concurrent || 1; | ||
return new Promise(function(resolve, reject) { | ||
function next(result) | ||
{ | ||
if (!first) { | ||
results.push(result); | ||
return new Promise(function (resolve, reject) { | ||
function next(result) { | ||
console.log('next', promises.length, (new Date()).getTime()); | ||
var concurrentPromises = []; | ||
var promise; | ||
if (!results) { | ||
results = []; | ||
} else { | ||
results = results.concat(result); | ||
} | ||
else { | ||
first = false; | ||
} | ||
if (promises.length) { | ||
var promise = promises.shift(); | ||
while (concurrentPromises.length < concurrent && promises.length) { | ||
var promise = promises.shift(); | ||
if (!promise || typeof promise.then !== 'function') { | ||
promise = Promise.resolve(); | ||
if (typeof promise === 'function') { | ||
promise = promise(); | ||
} | ||
if (!promise || typeof promise.then !== 'function') { | ||
promise = Promise.resolve(); | ||
} | ||
concurrentPromises.push(promise); | ||
} | ||
promise | ||
.then(next) | ||
.catch(reject); | ||
} | ||
else { | ||
Promise.all(concurrentPromises) | ||
.then(next) | ||
.catch(reject); | ||
} else { | ||
resolve(results); | ||
@@ -38,3 +46,3 @@ } | ||
}); | ||
} | ||
}; | ||
}; |
{ | ||
"name": "es6-promise-series", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Execute promises in series", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node node_modules/.bin/mocha index.test.js" | ||
}, | ||
@@ -23,3 +23,7 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/der-On/es6-promise-series" | ||
"homepage": "https://github.com/der-On/es6-promise-series", | ||
"devDependencies": { | ||
"expect.js": "^0.3.1", | ||
"mocha": "^2.3.4" | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/der-On/es6-promise-series.svg)](https://travis-ci.org/der-On/es6-promise-series) | ||
# ES6 Promise.series | ||
@@ -6,6 +8,12 @@ | ||
```javascript | ||
/** | ||
* Runs promises in series | ||
* @param {Array} list of promises or functions returning promises | ||
* @param {Number} (optional) number of concurrent promises, of omitted no concurrency will happen | ||
* @return {Promise} | ||
*/ | ||
Promise.series([ | ||
aPromise, | ||
anotherPromise | ||
]) | ||
], 4) | ||
.then(function (results) { | ||
@@ -12,0 +20,0 @@ ... |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
4174
6
107
1
22
2