promise-map-series
Advanced tools
Comparing version 0.1.0 to 0.2.0
# master | ||
# 0.2.0 | ||
* Do not take promises in `array` | ||
# 0.1.0 | ||
* Initial release |
27
index.js
var RSVP = require('rsvp') | ||
module.exports = function promiseMapSeries (arr, iterator, thisArg) { | ||
return RSVP.Promise.all(arr) | ||
.then(function (arr) { | ||
var results = new Array(arr.length) | ||
var index = 0 | ||
return arr.reduce(function (promise, item) { | ||
return promise.then(function () { | ||
return iterator.call(thisArg, item, index, arr) | ||
}) | ||
.then(function (result) { | ||
results[index++] = result | ||
}) | ||
}, RSVP.resolve()) | ||
.then(function () { | ||
return results | ||
module.exports = function promiseMapSeries (array, iterator, thisArg) { | ||
var results = new Array(array.length) | ||
var index = 0 | ||
return array.reduce(function (promise, item) { | ||
return promise.then(function () { | ||
return iterator.call(thisArg, item, index, array) | ||
}) | ||
.then(function (result) { | ||
results[index++] = result | ||
}) | ||
}, RSVP.resolve()) | ||
.then(function () { | ||
return results | ||
}) | ||
} |
{ | ||
"name": "promise-map-series", | ||
"description": "Map over array avoiding parallel execution, using promises", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Jo Liss <joliss42@gmail.com>", | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
@@ -27,5 +27,3 @@ # promise-map-series | ||
* **`array`**: An array of value or promises. All promises are resolved | ||
before running the iterator. If any are rejected, `mapSeries` returns a | ||
rejected promise immediately and does not call `iterator`. | ||
* **`array`**: An array of values (should not be promises). | ||
@@ -35,3 +33,3 @@ * **`iterator`**: Function that returns a promise or a value for the new | ||
returns a promise, then `iterator` will only be called for the next element | ||
once the promise is fulfilled. If the promise is rejected or `iterator` | ||
once that promise is fulfilled. If the promise is rejected or `iterator` | ||
throws an error, iteration will stop immediately and `mapSeries` returns a | ||
@@ -44,5 +42,4 @@ rejected promise. The `iterator` function receives three arguments: | ||
* **`nonPromiseArray`**: A copy of the original `array` with all promises | ||
resolved. | ||
* **`array`**: The original `array` argument. | ||
* **`thisArg`** (optional): Value to use as `this` when executing `iterator`. |
30
test.js
@@ -5,16 +5,8 @@ var test = require('tape') | ||
function addOne (item) { return item + 1 } | ||
test('mapSeries', function (t) { | ||
t.test('accepts values and promises in the array', function (t) { | ||
var arr = [1, RSVP.resolve(2)] | ||
mapSeries(arr, addOne).then(function (results) { | ||
t.deepEqual(results, [2, 3]) | ||
t.end() | ||
}) | ||
}) | ||
t.test('iterator is called in sequence for each item', function (t) { | ||
t.plan(6) | ||
var seq = 0 | ||
t.plan() | ||
mapSeries([0, 1], function (item) { | ||
@@ -32,3 +24,2 @@ t.equal(seq, item) | ||
t.deepEqual(results, ['foo', 'bar']) | ||
t.end() | ||
}) | ||
@@ -51,19 +42,8 @@ }) | ||
t.test('is rejected immediately if array contains rejected promise', function (t) { | ||
mapSeries([RSVP.reject('foo')], function (item) { | ||
t.fail('iterator should not be called') | ||
}) | ||
.then(function () { | ||
t.fail('should be rejected') | ||
}, function (err) { | ||
t.equal(err, 'foo') | ||
t.end() | ||
}) | ||
}) | ||
t.test('passes index and array argument to iterator', function (t) { | ||
t.plan(5) | ||
mapSeries([RSVP.resolve(42), 43], function (item, index, arr) { | ||
var arr = [42, 43] | ||
mapSeries(arr, function (item, index, array) { | ||
t.equal(item, index + 42) | ||
t.deepEqual(arr, [42, 43]) | ||
t.equal(array, arr) | ||
}).then(function (results) { | ||
@@ -70,0 +50,0 @@ t.deepEqual(results, [undefined, undefined]) |
Sorry, the diff of this file is not supported yet
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
5668
71
43