Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise_mtd

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise_mtd - npm Package Compare versions

Comparing version 2.0.7 to 2.1.0

.github/workflows/github-actions.yml

10

index.d.ts

@@ -17,2 +17,4 @@ type Unwrap<T> =

export function reduce<Result = any, Input = any>(data: Array<Input>, handler: (previousValue: Input, currentValue: Input, index: number, array: Array<Input>) => Promise<Input>, initialValue: Input): Promise<Input>;
export function transform<Result = any, Input = any>(data: Array<Input>, handler: (el: Input, index: number) => Promise<Result> | null | undefined): Promise<Array<Result>>;

@@ -22,6 +24,9 @@

export function find<Input = any>(data: Array<Input>, handler: (el: Input, index: number) => Promise<boolean>): Promise<Input>;
export function async_while(check: () => boolean, handler: () => void): Promise<null>;
export function asyncWhile(check: () => boolean, handler: () => void): Promise<null>;
export function parallel<Input = any>(data: Array<Input>, limit: number, handler: (el: Input, index: number) => Promise<void>): void;
export function parallel<Input = any>(data: Array<Input>, pool: number, handler: (el: Input, index: number) => Promise<void>): void;
export function parallel<Input = any>(data: Array<Input>, params: { pool: number }, handler: (el: Input, index: number) => Promise<void>): void;

@@ -31,7 +36,4 @@ export function all<T>(list: T): Promise<UnwrapListOrObject<T>>;

// export function all<Input = any>(data: Array<Input>): Promise<Array< Unwrap<Input> >>;
// export function all<Input = any>(data: { [k: string]: Input }): Promise<{ [k: string]: Unwrap<Input> }>;
export function setImmediate(): Promise<null>;
}

4

index.js

@@ -11,2 +11,6 @@ 'use strict';

promise_mtd.find = require('./promise_mtd/find.js');
promise_mtd.reduce = require('./promise_mtd/reduce.js');
promise_mtd.transform = require('./promise_mtd/transform.js');

@@ -13,0 +17,0 @@

{
"name": "promise_mtd",
"version": "2.0.7",
"description": "Set of methods allowing simplify work with Promises in cycle. Methods: forEach, map, while, transform, parallel",
"version": "2.1.0",
"description": "Set of methods allowing simplify work with Promises in cycle. Methods: forEach, map, find, filter, reduce, while, transform, parallel, all",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "npx jest"
},

@@ -23,2 +23,5 @@ "types": "./index.d.ts",

"foreach",
"reduce",
"filter",
"find",
"map",

@@ -70,3 +73,6 @@ "while",

"url": "https://github.com/dm-kamaev/promise_mtd.git"
},
"devDependencies": {
"jest": "27.2.0"
}
}

@@ -8,4 +8,17 @@ /**

module.exports = function (condition, action) {
if (arguments.length === 2) {
return v1(arguments[0], arguments[1]);
} else {
return v2(arguments[0]);
}
};
function v1(condition, action) {
var result = condition();
if (!result) {
return;
}
var next = () => {

@@ -19,3 +32,14 @@ if (!condition()) {

return action().then(next);
};
}
function v2(action) {
var next = (res) => {
if (res) {
return action().then(next);
} else {
return;
}
};
return action().then(next);
}
'use strict';
/**
* map –– call promise step by step, and push res in array
* filter –– call promise step by step, and push res in array
* @param {Array<any>} data

@@ -13,7 +13,8 @@ * @param {function(el, index)}

for (let i = 0, l = data.length; i < l; i++) {
let el = data[i];
start = start.then(() => {
return promiseHandler(data[i], i);
return promiseHandler(el, i);
}).then((res) => {
if (res) {
list.push(res);
list.push(el);
} else {

@@ -25,13 +26,2 @@ return;

return start.then(() => list);
};
// EXAMPLE:
// module.exports([ 0, 1, 2, 3 ], function(time, i) {
// return new Promise((resolve, reject) => {
// setTimeout(function() {
// resolve(i);
// // reject(new Error('stop'));
// }, time*1000);
// });
// }).then(res => console.log(res)).catch(err => console.log(err));
};

@@ -21,13 +21,1 @@ 'use strict';

};
// EXAMPLE:
// promiseMtd.map([3000, 2000, 1000], function(time, i) {
// return new Promise((resolve, reject) => {
// setTimeout(function() {
// console.log('HERE=', time, i);
// resolve('Result='+i);
// // reject(new Error('stop'));
// }, time);
// });
// }).then(res => console.log(res)).catch(err => console.log(err));

@@ -6,10 +6,18 @@ 'use strict';

* @param {Array<any>} data
* @param {number} limit
* @param {number | { pool: number }} params
* @param {function(el, i): Promise } promise_handler]
* @return {Promise}
*/
module.exports = function (data, limit, promise_handler) {
if (!limit) {
throw new Error('required limit');
module.exports = function (data, params, promise_handler) {
let pool;
if (typeof params === 'number') {
console.warn('Warning: You shoulds use { pool: ' + params+' } instead of simple number');
pool = params;
} else {
pool = params.pool;
}
if (!pool) {
throw new Error('required pool');
}
var next = () => {

@@ -25,6 +33,6 @@ if (data[current]) {

var current = limit;
var current = pool;
var arr = [];
for (let i = 0, l = data.length; i < l; i++) {
if (i < limit) {
if (i < pool) {
arr.push(

@@ -38,19 +46,2 @@ Promise.resolve().then(() => promise_handler(data[i], i)).then(next)

// EXAMPLE
// void async function() {
// try {
// await promise_mthds.parallel([ 3000, 3000, 3000, 2000, 2000, 2000, 1000], 3, async function(t, i) {
// return new Promise((resolve) => {
// // if (i === 4) {
// // throw new Error('stop');
// // }
// setTimeout(() => {
// console.log(t);
// resolve();
// }, t);
// });
// });
// } catch (err) {
// console.log('Raise', err);
// }
// }();
# promise_mtd
Set of methods allowing to simplify work with promises in cycle. The library has support TypeScript.
* Implementation of ```forEach```, ```map```, ```filter``` for working with array data when it's needed to apply asynchronous function to each element.
* Implementation of ```forEach```, ```map```, ```filter```, ```reduce```, ```find``` for working with array data when it's needed to apply asynchronous function to each element.
* Method ```transform``` allows to iterate asynchronously over an array similarly to ```map```, but also it can skip unnecessary data.

@@ -17,4 +17,4 @@ * Implementation of cycle while as ```asyncWhile```(while is reserved word) for using with promise.

### foreach(Array<any>, Function(el, index)) || forEach(Array<any>, Function(el, index))
```Foreach``` over promises serially
### forEach(Array, function(el, index))
```forEach``` over promises serially
```js

@@ -27,3 +27,3 @@ const promiseMtd = require('promise_mtd');

setTimeout(function() {
console.log(el);
console.log(el); // 300 then 200 then 100
resolve();

@@ -37,4 +37,4 @@ }, el+i);

### map(Array<any>, Function(el, index): Promise<any>)
```Map``` over promises serially
### map(Array<any>, function(el, index): Promise<any>): Promise<Array<any>>
```map``` over promises serially
```js

@@ -44,3 +44,3 @@ const promiseMtd = require('promise_mtd');

void async function () {
let res = await promiseMtd.map([ 300, 200, 100], async function (el, i) {
const res = await promiseMtd.map([ 300, 200, 100], async function (el, i) {
return new Promise((resolve, reject) => {

@@ -57,4 +57,4 @@ setTimeout(function() {

### filter(Array<any>, Function(el, index): Promise<Boolean>)
```Filter```
### reduce(Array, function(previousValue, currentValue, index, array): Promise)
```reduce``` over promises serially
```js

@@ -64,14 +64,49 @@ const promiseMtd = require('promise_mtd');

void async function () {
let res = await promiseMtd.filter([ 0, 1, 2, 3 ], function(time, i) {
const result = await promiseMtd.reduce([0, 1, 2, 3, 4], function (previousValue, currentValue, index, array) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(previousValue + currentValue);
}, currentValue*1000);
});
}, 0);
}();
console.log(result); // 10
```
### filter(Array<any>, function(el, index): Promise<Boolean>): Array<any>
```filter``` over promises serially
```js
const promiseMtd = require('promise_mtd');
void async function () {
const res = await promiseMtd.filter([ 1, 2, 3, 4 ], function(time, i) {
return new Promise((resolve, reject) => {
setTimeout(function() {
resolve(Boolean(i));
resolve(i % 2 === 0);
}, time * 1000);
});
});
console.log(res); // [ 1, 2, 3 ]
console.log(res); // [ 2, 4 ]
}();
```
### parallel(Array<any>, Function(el, index))
### find(Array, function(el, index): Promise)
```find``` over promises serially
```js
const promiseMtd = require('promise_mtd');
void async function () {
const result = await promiseMtd.find([0, 1, 2, 3, 4], function (previousValue, currentValue, index, array) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(el === 2);
}, el*1000);
});
});
}();
console.log(result); // 2
```
### parallel(Array<any>, { pool: number }, function(el, index))
Equivalent of ```Promise.all``` but with limit

@@ -82,3 +117,3 @@ ```js

void async function() {
await promiseMtd.parallel([ 3000, 3000, 3000, 2000, 2000, 2000, 1000], 3, async function(el, i) {
await promiseMtd.parallel([ 3000, 3000, 3000, 2000, 2000, 2000, 1000], { pool: 3 }, async function(el, i) {
return new Promise((resolve) => {

@@ -96,3 +131,3 @@ setTimeout(() => {

### transform(Array<any>, Function(el, index))
### transform(Array<any>, function(el, index): Promise<any>): Array
Iterating over an array and filter over promises

@@ -116,4 +151,4 @@ ```js

### asyncWhile(condition: Function(): Boolean, Function)
```While``` over promises serially
### asyncWhile(condition: () => boolean, function()) | asyncWhile(condition: () => Promise<boolean>)
```while``` over promises serially
```js

@@ -124,7 +159,26 @@ const promiseMtd = require('promise_mtd');

let i = 0;
await promiseMtd.asyncWhile(() => i < 5, async function () {
console.log(i);
i++;
let result = [];
await promiseMtd.asyncWhile(() => i < 10, function () {
return new Promise((resolve) => {
setTimeout(() => {
result.push(i++);
resolve();
}, i*1000);
});
});
console.log(i); // 5
console.log(result); // [0,1,2,3,4,5,5,6,7,8,9]
// OR
i = 0
result = [];
await promiseMtd.asyncWhile(function () {
return new Promise((resolve) => {
setTimeout(() => {
result.push(i++);
resolve(i < 10);
}, i*1000);
});
});
console.log(result); // [0,1,2,3,4,5,5,6,7,8,9]
}();

@@ -135,3 +189,3 @@ ```

### all(data: Array<Promise> | Object<{ key: Promise }>): Array<any> | Object<{ key: any }>
```All``` over promises serially
```All``` like ```Promise.all``` but it can handle object
```js

@@ -161,2 +215,8 @@ const promiseMtd = require('promise_mtd');

}();
```
## Tests
```sh
npm test
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc