feture: promise.mapSeries

This commit is contained in:
harttle
2016-10-31 23:45:41 +08:00
parent 6dd836f157
commit 9961afa3dd
3 changed files with 69 additions and 12 deletions
+21 -5
View File
@@ -6,14 +6,30 @@ const Promise = require('any-promise');
* @param {Array} iteratee returns a new promise.
* The iteratee is invoked with three arguments: (value, index, iterable).
*/
function someSeries(iterable, iteratee) {
function anySeries(iterable, iteratee) {
var ret = Promise.reject(new Error('init'));
iterable.forEach(function(item, idx) {
ret = ret
.then(x => x)
.catch(e => iteratee(item, idx, iterable));
ret = ret.catch(e => iteratee(item, idx, iterable));
});
return ret;
}
exports.someSeries = someSeries;
/*
* Call functions in serial until someone rejected.
* @param {Array} iterable the array to iterate with.
* @param {Array} iteratee returns a new promise.
* The iteratee is invoked with three arguments: (value, index, iterable).
*/
function mapSeries(iterable, iteratee) {
var ret = Promise.resolve('init');
var result = [];
iterable.forEach(function(item, idx) {
ret = ret
.then(() => iteratee(item, idx, iterable))
.then(x => result.push(x));
});
return ret.then(() => result);
}
exports.anySeries = anySeries;
exports.mapSeries = mapSeries;