@kev_nz/async-tools
Advanced tools
Comparing version
{ | ||
"name": "@kev_nz/async-tools", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Async tools - a collection of utility functions for working with async/await code.", | ||
@@ -14,3 +14,4 @@ "main": "src/index.js", | ||
"predocs": " jest --coverage --coverageDirectory=./dist/coverage ", | ||
"docs": "publisher" | ||
"docs": "publisher", | ||
"jsdox": "jsdoc ./src/*.js --configure .jsdoc.json --verbose" | ||
}, | ||
@@ -44,4 +45,7 @@ "repository": { | ||
"eslint": "^5.16.0", | ||
"jest": "^24.6.0" | ||
"jest": "^24.6.0", | ||
"jsdoc": "^3.5.5", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"minami": "^1.2.3" | ||
} | ||
} |
@@ -135,2 +135,14 @@ # Async Tools | ||
### Timeout | ||
Take number of functions and compose them together. | ||
```javascript | ||
const { timeout } = require('@kev_nz/async-tools') | ||
(async () => { | ||
const result = await timeout(() => thingThatIsLongRunning()) | ||
})() | ||
`` | ||
### Test Coverage | ||
@@ -137,0 +149,0 @@ |
const reducer = require('./reducer') | ||
/** | ||
* Composer | ||
* | ||
* @param {*} funcs - The cunctions to chain | ||
* @returns a function chain | ||
* | ||
* @example | ||
* | ||
* const asyncChain = composer( | ||
* anAsyncFunction, | ||
* anotherAsyncFunction, | ||
* anAsyncFunction | ||
* ) | ||
* const finalValue = await asyncChain(0) | ||
*/ | ||
module.exports = (...funcs) => { | ||
return val => reducer(val, ...funcs) | ||
} |
@@ -0,2 +1,13 @@ | ||
/** | ||
* Executes an async functions that will provide a | ||
* delay for the amount of time passed in | ||
* | ||
* @async | ||
* @function delay | ||
* @param {number} time - The duration in milliseconds | ||
* @example | ||
* // wait 20 milliseconds | ||
* await delay(20) | ||
*/ | ||
module.exports = time => | ||
new Promise(resolve => setTimeout(() => resolve(), time)) |
const mapper = require('./mapper') | ||
/** | ||
* Execute an async function over an iterable collection one at a time | ||
* | ||
* @async | ||
* @function each | ||
* @param {*} iterable - An iterable collection | ||
* @param {function} mappingFunction - The async function to execute | ||
* @returns {*} - The mapped results | ||
* | ||
* @example | ||
* | ||
* const results = await each( | ||
* [1,2,3,4,5], | ||
* asyncFunction | ||
* ) | ||
*/ | ||
module.exports = (iterable, mappingFunction) => { | ||
return mapper(iterable, mappingFunction, 1) | ||
} |
@@ -0,3 +1,20 @@ | ||
/** | ||
* Execute an async function over an iterable collection | ||
* | ||
* @function holder | ||
* @param {function} func - The function that will be called | ||
* @param {*} params - any parameters required for the function | ||
* @returns {function} - A function that takes a value and executes | ||
* the passed function with the value and other parameters | ||
* @example | ||
* | ||
* const asyncChain = composer( | ||
* anAsyncFunction, | ||
* holder(anotherAsyncFunction, 3), | ||
* anAsyncFunction | ||
* ) | ||
* const finalValue = await asyncChain(0) | ||
*/ | ||
module.exports = (func, ...params) => { | ||
return val => func(val, ...params) | ||
} |
@@ -0,1 +1,17 @@ | ||
/** | ||
* Execute an async function over an iterable collection | ||
* | ||
* @async | ||
* @function mapper | ||
* @param {*} iterable- An iterable collection | ||
* @param {function} mappingFunction - The async mapping function to execute | ||
* @param {number} [concurrency=Infinity] - The max number of concurrent calls | ||
* @returns {*} - The mapped results | ||
* @example | ||
* | ||
* const results = await mapper( | ||
* [1,2,3,4,5], | ||
* asyncFunction | ||
* ) | ||
*/ | ||
const mapper = (iterable, mappingFunction, concurrency = Infinity) => | ||
@@ -2,0 +18,0 @@ new Promise((resolve, reject) => { |
@@ -0,1 +1,19 @@ | ||
/** | ||
* Execute an array of async functions in order | ||
* passing the result of each function to the next | ||
* | ||
* @async | ||
* @function reducer | ||
* @param {*} startValue - The starting value | ||
* @param {function[]} funcs - The async functions to execute | ||
* @returns {*} - The final value from the functions | ||
* | ||
* @example | ||
* | ||
* const results = await reducer(0, | ||
* asyncFunction, | ||
* asyncFunction2, | ||
* asyncFunction3 | ||
* ) | ||
*/ | ||
module.exports = (startValue, ...funcs) => | ||
@@ -2,0 +20,0 @@ new Promise((resolve, reject) => { |
20437
27.37%17
13.33%492
44.28%151
8.63%8
60%