New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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 3.1.0 to 3.2.0

2

index.d.ts

@@ -34,3 +34,3 @@ type Unwrap<T> =

export function retry<T extends (...args: any) => any, E extends { new(...arg: any): any }>(fn: T, settings: { attemp: number, delay?: { ms: number }, ifError?: E }): (...argv: Parameters<T>) => Promise<ReturnType<T>>;
export function retry<T extends (...args: any) => Promise<any>, E extends { new(...arg: any): any }>(fn: T, settings: { attemp: number, delay?: { ms: number }, ifError?: E }): (...argv: Parameters<T>) => Promise<ReturnType<T>>;

@@ -37,0 +37,0 @@ export function all<T>(list: T): Promise<UnwrapListOrObject<T>>;

{
"name": "promise_mtd",
"version": "3.1.0",
"version": "3.2.0",
"description": "Set of methods allowing simplify work with promises in cycle such as: forEach, map, find, filter, reduce, while, transform. Besides there are methods for comfortable work with promises or asynchronous operations - all, retry, timeout.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -8,17 +8,21 @@ # promise_mtd

## Methods:
Methods:
* `forEach`
* `forEachParallel`
* `map`
* `mapParallel`
* `filter`
* `filterParallel`
* `find`
* `findParallel`
* `reduce`
* `while_`
* `transform`
* `all`
* `retry`
* `timeout`
- [forEach](#foreach)
- [forEachParallel](#foreachparallel)
- [map](#map)
- [mapParallel](#mapparallel)
- [reduce](#reduce)
- [filter](#filter)
- [filterParallel](#filterparallel)
- [find](#find)
- [findParallel](#findparallel)
- [transform](#transform)
- [transformParallel](#transformparallel)
- [while](#while_)
- [all](#all)
- [retry](#retry)
- [timeout](#timeout)

@@ -38,3 +42,4 @@ The library has no dependencies 😀

### forEach(Array, function(el, index))
### forEach
`forEach(Array, function(el, index))`
It's analog of standard method `forEach` of array, but for asynchronous sequent actions based on promises.

@@ -48,3 +53,4 @@ ```js

### forEachParallel(Array, { pool: number }, function(el, index))
### forEachParallel
`forEachParallel(Array, { pool: number }, function(el, index))`
It's the same method as `forEach`, but actions are executed in parallel. The `pool` is the maximum number of promises executed in parallel. In other words, number of promises which may be executed simutalneously is equal or less than value of `pool`.

@@ -60,3 +66,4 @@ ```js

### map(Array<any>, function(el, index): Promise<any>): Promise<Array<any>>
### map
`map(Array<any>, function(el, index): Promise<any>): Promise<Array<any>>`
It's analog of standard method `map` of array, but for asynchronous sequent actions based on promises.

@@ -71,3 +78,4 @@ ```js

### mapParallel(Array<any>, { pool: number }, function(el, index): Promise<any>): Promise<Array<any>>
### mapParallel
`mapParallel(Array<any>, { pool: number }, function(el, index): Promise<any>): Promise<Array>`
It's the same method as `map`, but actions are executed in parallel. The `pool` is the maximum number of promises executed in parallel. In other words, number of promises which may be executed simutalneously is equal or less than value of `pool`.

@@ -82,4 +90,4 @@ ```js

### reduce(Array, function(previousValue, currentValue, index, array): Promise)
### reduce
`reduce(Array, function(previousValue, currentValue, index, array): Promise)`
It's analog of standard method `reduce` of array, but for asynchronous sequent actions based on promises.

@@ -95,3 +103,4 @@ ```js

### filter(Array<any>, function(el, index): Promise<Boolean>): Array<any>
### filter
`filter(Array<any>, function(el, index): Promise<Boolean>): Array<any>`
It's analog of standard method `filter` of array, but for asynchronous sequent actions based on promises.

@@ -106,3 +115,4 @@ ```js

### filterParallel(Array, { pool: number }, function(el, index): Promise<Boolean>): Array<any>
### filterParallel
`filterParallel(Array, { pool: number }, function(el, index): Promise<Boolean>): Array<any>`
It's the same method as `filter`, but actions are executed in parallel. The `pool` is the maximum number of promises executed in parallel. In other words, number of promises which may be executed simutalneously is equal or less than value of `pool`.

@@ -117,3 +127,4 @@ ```js

### find(Array, function(el, index): Promise): any
### find
`find(Array, function(el, index): Promise): any`
It's analog of standard method `find` of array, but for asynchronous sequent actions based on promises.

@@ -128,3 +139,4 @@ ```js

### findParallel(Array, { pool: number }, function(el, index): Promise): any
### findParallel
`findParallel(Array, { pool: number }, function(el, index): Promise): any`
It's the same method as `find`, but actions are executed in parallel. The `pool` is the maximum number of promises executed in parallel. In other words, number of promises which may be executed simutalneously is equal or less than value of `pool`.

@@ -140,3 +152,4 @@ ```js

### transform(Array, function(el, index): Promise): Array
### transform
`transform(Array, function(el, index): Promise): Array`
Method `transform` allows to iterate asynchronously over an array similarly to `map`, but also it can skip unnecessary data.

@@ -153,3 +166,4 @@ ```js

### transformParallel(Array, { pool: number }, function(el, index): Promise): Array
### transformParallel
`transformParallel(Array, { pool: number }, function(el, index): Promise): Array`
It's the same method as `transform`, but actions are executed in parallel. The `pool` is the maximum number of promises executed in parallel. In other words, number of promises which may be executed simutalneously is equal or less than value of `pool`.

@@ -167,3 +181,4 @@ ```js

### while_(condition: () => boolean, function(), params?: { limit: number }) | while_(condition: () => Promise<boolean>, params?: { limit: number })
### while
`while_(condition: () => boolean, function(), params?: { limit: number }) | while_(condition: () => Promise<boolean>, params?: { limit: number })`
Implementation of cycle `while` as `while_` (`while` is reserved word in JavaScript) for using with promise.

@@ -203,3 +218,4 @@ `while_` iterates over promises sequentially. This method supports limit of iterations (protection from forever cycle) via third parameter.

### all(data: Array | Object<{ key: Promise }>): Array<any> | Object<{ key: any }>
### all
`all(data: Array | Object<{ key: Promise }>): Array<any> | Object<{ key: any }>`
Method `all` allows to run concurrently promises similarly to method `Promise.all`, but supports receipt of parameter such as object `{ k1: Promise, k2: Promise }` not only as array.

@@ -222,3 +238,4 @@ ```js

### retry(fn: Function, { attempt: number, delay?: { ms: number }, ifError: Error }): Function
### retry
`retry(fn: Function, { attempt: number, delay?: { ms: number }, ifError: Error }): Function`
This method allows to create wrapper for asynchronous function which repeats calling if the function returns an error.

@@ -260,3 +277,4 @@ * `attempt`is parameter which sets number of attempts for executing.

### timeout(ms: number)
### timeout
`timeout(ms: number)`
It's promifisified version for `setTimeout`.

@@ -263,0 +281,0 @@ ```js

@@ -22,32 +22,24 @@ 'use strict';

settings.delay = settings.delay || { ms: null };
const exec = function (...argv) {
try {
return fn.apply(fn, argv);
} catch (err) {
const call = () => exec.apply(exec, argv);
if (settings.ifError) {
if (err instanceof settings.ifError && settings.attemp - 1) {
return repeat(settings, call);
const iterationWithRetry = function (...argv) {
return fn.apply(fn, argv).catch(err => {
if ((settings.attemp - 1) < 0) {
throw err;
}
if (!((settings.ifError && err instanceof settings.ifError) || !settings.ifError)) {
throw err;
}
return Promise.resolve().then(() => {
if (settings.delay.ms) {
return timeout(settings.delay.ms);
}
} else {
if (settings.attemp - 1) {
return repeat(settings, call);
}
}
throw err;
}
}).then(() => {
settings.attemp--;
return iterationWithRetry.apply(iterationWithRetry, argv)
});
});
};
return exec;
return iterationWithRetry;
}
function repeat(settings, call) {
const delay = settings.delay;
settings.attemp--;
if (delay.ms) {
return timeout(delay.ms).then(call);
}
return call();
}

@@ -16,7 +16,9 @@ const { retry, timeout } = require('../../index.js');

function main() {
if (++counter < 3) {
throw new Error('STOP');
}
return timeout(100);
};
return new Promise((resolve, reject) => {
if (++counter < 3) {
return reject(new Error('STOP'));
}
resolve();
}).then(() => timeout(100));
}
const fn = retry(main, { attemp: 3 });

@@ -30,7 +32,9 @@ await fn();

function main(a, b) {
if (++counter < 3) {
throw new Error('STOP');
}
return timeout(100).then(() => a+b);
};
return new Promise((resolve, reject) => {
if (++counter < 3) {
return reject(new Error('STOP'));
}
resolve();
}).then(() => timeout(100)).then(() => a+b);
}
const fn = retry(main, { attemp: 3 });

@@ -44,40 +48,38 @@ expect(await fn(1, 2)).toBe(3);

function main() {
if (++counter < 10) {
throw new Error('STOP');
}
return timeout(100);
};
return new Promise((resolve, reject) => {
if (++counter < 10) {
return reject(new Error('STOP'));
}
resolve();
}).then(() => timeout(100));
}
const fn = retry(main, { attemp: 3 });
expect.assertions(1);
try {
fn();
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
await expect(fn).rejects.toThrow(Error);
});
it('call with tries [success version]', async function () {
let counter = 0;
function main() {
if (++counter < 3) {
throw new Error('STOP');
}
return timeout(100);
};
const fn = retry(main, { attemp: 3 });
await fn();
expect(counter).toBe(3);
});
// it('call with tries [success version]', async function () {
// let counter = 0;
// function main() {
// if (++counter < 3) {
// throw new Error('STOP');
// }
// return timeout(100);
// };
// const fn = retry(main, { attemp: 3 });
// await fn();
// expect(counter).toBe(3);
// });
it('call with tries and timeout: we catch checked specific type error [success version]', async function () {
let counter = 0;
class CustomError extends Error {}
function main() {
if (++counter < 3) {
throw new CustomError('STOP');
}
return timeout(100);
};
return new Promise((resolve, reject) => {
if (++counter < 3) {
return reject(new CustomError('STOP'));
}
resolve();
}).then(() => timeout(100));
}

@@ -94,10 +96,11 @@ const fn = retry(main, { attemp: 3, delay: { ms: 200 }, ifError: CustomError, });

function main() {
throw new CustomError2('STOP');
return timeout(100);
};
return new Promise((_, reject) => {
return reject(new CustomError2('STOP'));
}).then(() => timeout(100));
}
const fn = retry(main, { attemp: 3, delay: { ms: 200 }, ifError: CustomError, });
await expect(fn).toThrow(CustomError2);
const fn = retry(main, { attemp: 3, delay: { ms: 200 }, ifError: CustomError });
await expect(fn).rejects.toThrow(CustomError2);
});
});
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