promise_mtd
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -21,5 +21,6 @@ type Unwrap<T> = | ||
export function transform<Result = any, Input = any>(data: Array<Input>, handler: (el: Input, index: number) => Promise<Result> | null | undefined): Promise<Array<Result>>; | ||
export function transformParallel<Result = any, Input = any>(data: Array<Input>, params: { pool: number }, handler: (el: Input, index: number) => Promise<Result> | null | undefined): Promise<Array<Result>>; | ||
export function transform<Result = any, Input = any>(data: Array<Input>, handler: (el: Input, index: number) => Promise<Result> | null | undefined): Promise<Array<NonNullable<Result>>>; | ||
export function transformParallel<Result = any, Input = any>(data: Array<Input>, params: { pool: number }, handler: (el: Input, index: number) => Promise<Result> | null | undefined): Promise<Array<NonNullable<Result>>>; | ||
export function filter<Input = any>(data: Array<Input>, handler: (el: Input, index: number) => Promise<boolean>): Promise<Array<Input>>; | ||
@@ -26,0 +27,0 @@ export function filterParallel<Input = any>(data: Array<Input>, params: { pool: number }, handler: (el: Input, index: number) => Promise<boolean>): Promise<Array<Input>>; |
{ | ||
"name": "promise_mtd", | ||
"version": "3.0.1", | ||
"version": "3.1.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.", | ||
@@ -77,4 +77,5 @@ "main": "index.js", | ||
"coverage-badges": "1.0.7", | ||
"jest": "27.5.1" | ||
"jest": "27.5.1", | ||
"ts-node": "10.9.1" | ||
} | ||
} |
@@ -156,3 +156,3 @@ # promise_mtd | ||
### 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. | ||
Implementation of cycle `while` as `while_` (`while` is reserved word in JavaScript) for using with promise. | ||
`while_` iterates over promises sequentially. This method supports limit of iterations (protection from forever cycle) via third parameter. | ||
@@ -159,0 +159,0 @@ |
'use strict'; | ||
const Stop = require('../lib/Stop.js'); | ||
/** | ||
@@ -20,7 +18,8 @@ * findParallel | ||
let found = false; | ||
let result = undefined; | ||
let stop = false; | ||
const handler = (res, el) => { | ||
if (res) { | ||
found = true; | ||
throw new Stop(el); | ||
if (!stop && res) { | ||
stop = true; | ||
result = el; | ||
} | ||
@@ -30,3 +29,3 @@ } | ||
const next = () => { | ||
if (found) { | ||
if (stop) { | ||
return; | ||
@@ -44,4 +43,4 @@ } else if (current in data) { | ||
var current = pool; | ||
var arr = []; | ||
let current = pool; | ||
const arr = []; | ||
for (let i = 0, l = data.length; i < l; i++) { | ||
@@ -57,9 +56,3 @@ if (i < pool) { | ||
return Promise.all(arr).catch(err => { | ||
if (err instanceof Stop) { | ||
return err.value; | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
return Promise.all(arr).then(() => result) | ||
}; |
@@ -14,6 +14,40 @@ | ||
}); | ||
expect(result).toEqual(2); | ||
}); | ||
it('find 2 without timeout', async function () { | ||
const result = await findParallel([1,2,3], { pool: 3 }, async function (el, i) { | ||
return el === 2; | ||
}); | ||
expect(result).toEqual(2); | ||
}); | ||
it('find first 2', async function () { | ||
const list = [{ id: 'a', val: 0 }, { id: 'b', val: 1 }, { id: 'c', val: 2 }, { id: 'd', val: 3 }, { id: 'e', val: 4 }, { id: 'f', val: 2 }, { id: 'g', val: 10 },] | ||
const result = await findParallel(list, { pool: 2 }, function (el, i) { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(el.val === 2); | ||
}, (list.length-i)*1000); | ||
}); | ||
}); | ||
expect(result.val).toEqual(2); | ||
expect(result.id).toEqual('c'); | ||
}); | ||
it('find undefined', async function () { | ||
const result = await findParallel([0, 1, 2, 3, 4], { pool: 2 }, function (el) { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(el === 100); | ||
}, el*1000); | ||
}); | ||
}); | ||
expect(result).toBeUndefined(); | ||
}); | ||
it('reject error', async function () { | ||
@@ -20,0 +54,0 @@ expect.assertions(1); |
'use strict'; | ||
const Stop = require('../lib/Stop.js'); | ||
/** | ||
@@ -12,10 +10,17 @@ * find –– call promise step by step, and push res in array | ||
module.exports = function (data, promiseHandler) { | ||
var start = Promise.resolve(); | ||
let start = Promise.resolve(); | ||
let stop = false; | ||
let found = undefined; | ||
for (let i = 0, l = data.length; i < l; i++) { | ||
let el = data[i]; | ||
start = start.then(() => { | ||
if (stop) { | ||
return; | ||
} | ||
return promiseHandler(el, i); | ||
}).then((res) => { | ||
if (res) { | ||
throw new Stop(el); | ||
if (!stop && res) { | ||
found = el; | ||
stop = true; | ||
} | ||
@@ -25,11 +30,5 @@ }); | ||
return start.then(() => list).catch(err => { | ||
if (err instanceof Stop) { | ||
return err.value; | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
return start.then(() => found); | ||
}; | ||
@@ -18,2 +18,36 @@ | ||
it('find 2 without timeout', async function () { | ||
const result = await find([1,2,3], async function (el, i) { | ||
return el === 2; | ||
}); | ||
expect(result).toEqual(2); | ||
}); | ||
it('find first 2', async function () { | ||
const list = [{ id: 'a', val: 0 }, { id: 'b', val: 1 }, { id: 'c', val: 2 }, { id: 'd', val: 3 }, { id: 'e', val: 4 }, { id: 'f', val: 2 }, { id: 'g', val: 10 },] | ||
const result = await find(list, function (el, i) { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(el.val === 2); | ||
}, (list.length-i)*1000); | ||
}); | ||
}); | ||
expect(result.val).toEqual(2); | ||
expect(result.id).toEqual('c'); | ||
}); | ||
it('find undefined', async function () { | ||
const result = await find([0, 1, 2, 3, 4], function (el) { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(el === 100); | ||
}, el*1000); | ||
}); | ||
}); | ||
expect(result).toBeUndefined(); | ||
}); | ||
it('reject error', async function () { | ||
@@ -20,0 +54,0 @@ expect.assertions(1); |
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
50381
44
1207
3