🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

sequential-promise-map

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sequential-promise-map - npm Package Compare versions

Comparing version

to
1.2.0

2

package.json
{
"name": "sequential-promise-map",
"version": "1.1.0",
"version": "1.2.0",
"description": "A simple sequential version of Promise.map",

@@ -5,0 +5,0 @@ "main": "src/sequential-promise-map.js",

@@ -16,8 +16,7 @@ # sequential-promise-map

```js
sequentialPromiseMap(arrayOfValues, promiseGenerator).then(....)
sequentialPromiseMap(arrayOfValues, promiseGenerator, /*batchSize*/).then(....)
```
the `promiseGenerator` parameter should be a function:
* `arrayOfValues` should be an `Array` of elements that will be asynchronously processed
* `promiseGenerator` parameter should be a function to process each element of the array, receiving up to three arguments:
```js

@@ -28,4 +27,5 @@ promiseGenerator = function(currentElement, indexOfCurrent, originalArray) {

```
* `batchSize` is an optional argument, a positive integer specifying the maximum batch size for parallel processing. If not set, the elements will be processed one by one. Set this to a value greater than 1 to allow for parallelisation.
The function will resolve will contain an array of Promise results, in the same order as the arguments, or reject with the first rejection.
The function will resolve will contain an array of `Promise` results, in the same order as the arguments, or reject with the first rejection.

@@ -32,0 +32,0 @@

@@ -1,2 +0,2 @@

module.exports = function sequentialPromiseMap(array, generator) {
module.exports = function sequentialPromiseMap(array, generator, batchArg) {
'use strict';

@@ -9,18 +9,22 @@ if (!Array.isArray(array)) {

}
let index = 0;
const results = [],
items = (array && array.slice()) || [],
sendSingle = function (item) {
return generator(item, index++, array)
.then(result => results.push(result));
if (batchArg && (!Number.isInteger(batchArg) || batchArg < 1)) {
throw new Error('the third argument must be undefined or a positive integer');
}
const batchSize = batchArg || 1,
results = [],
processBatch = function (batch, startIndex) {
const shiftIndexAndProcess = (element, index) => generator(element, index + startIndex, array);
return Promise.all(batch.map(shiftIndexAndProcess));
},
sendAll = function () {
if (!items.length) {
return Promise.resolve(results);
} else {
return sendSingle(items.shift())
.then(sendAll);
sendNextBatch = function (startIndex) {
if (startIndex >= array.length) {
return Promise.resolve();
}
const next = array.slice(startIndex, startIndex + batchSize);
return processBatch(next, startIndex)
.then(nextResults => results.push.apply(results, nextResults))
.then(() => sendNextBatch(startIndex + batchSize));
};
return sendAll();
return sendNextBatch(0)
.then(() => results);
};