|
| 1 | +# async-array-utils |
| 2 | +A collection of array-related async utilities. |
| 3 | + |
| 4 | +## tl;dr |
| 5 | +* Install by executing `npm install @wojtekmaj/async-array-utils` or `yarn add @wojtekmaj/async-array-utils`. |
| 6 | +* Import by adding `import * as asyncArrayUtils from '@wojtekmaj/async-array-utils'`. |
| 7 | +* Do stuff with it! |
| 8 | + ```js |
| 9 | + const asyncMappedArr = await asyncMap([1, 2, 3], async (x) => x * 2); |
| 10 | + ``` |
| 11 | + |
| 12 | +## User guide |
| 13 | + |
| 14 | +### General utils |
| 15 | + |
| 16 | +#### `asyncMap()` |
| 17 | + |
| 18 | +Creates a new array populated with the results of calling a provided asynchronous function on every element in the calling array. |
| 19 | + |
| 20 | +Note: For optimization purposes, all iterations are ran concurrently. If you rely on any side effects, consider `asyncMapStrict()` instead. |
| 21 | + |
| 22 | +##### Sample usage |
| 23 | + |
| 24 | +```js |
| 25 | +import { asyncMap } from '@wojtekmaj/async-array-utils'; |
| 26 | + |
| 27 | +const asyncMappedArr = await asyncMap([1, 2, 3], async (el, index) => el * 2); // [2, 4, 6] |
| 28 | +``` |
| 29 | + |
| 30 | +#### `asyncMapStrict()` |
| 31 | + |
| 32 | +Like `asyncMap()`, but runs iterations non-concurrently. |
| 33 | + |
| 34 | +##### Sample usage |
| 35 | + |
| 36 | +```js |
| 37 | +import { asyncMapStrict } from '@wojtekmaj/async-array-utils'; |
| 38 | + |
| 39 | +const indexes = []; |
| 40 | +await asyncMapStrict([1, 2, 3], async (el, index) => { indexes.push(index); return el * 2; }); // [2, 4, 6] |
| 41 | +console.log(indexes); // [0, 1, 2] |
| 42 | +``` |
| 43 | + |
| 44 | +#### `asyncForEach()` |
| 45 | + |
| 46 | +Executes a provided asynchronous function once for each array element. |
| 47 | + |
| 48 | +Note: For optimization purposes, all iterations are ran concurrently. If you rely on any side effects, consider `asyncForEachStrict()` instead. |
| 49 | + |
| 50 | +##### Sample usage |
| 51 | + |
| 52 | +```js |
| 53 | +import { asyncForEach } from '@wojtekmaj/async-array-utils'; |
| 54 | + |
| 55 | +await asyncForEach( |
| 56 | + [1, 2, 3], |
| 57 | + async (el) => { console.log(el * 2); } |
| 58 | +); // undefined; 3 console.logs |
| 59 | +``` |
| 60 | + |
| 61 | +#### `asyncForEachStrict()` |
| 62 | + |
| 63 | +Like `asyncForEach()`, but runs iterations non-concurrently. |
| 64 | + |
| 65 | +##### Sample usage |
| 66 | + |
| 67 | +```js |
| 68 | +import { asyncForEachStrict } from '@wojtekmaj/async-array-utils'; |
| 69 | + |
| 70 | +const indexes = []; |
| 71 | +await asyncForEachStrict( |
| 72 | + [1, 2, 3], |
| 73 | + async (el) => { indexes.push(index); console.log(el * 2); } |
| 74 | +); // undefined; 3 console.logs |
| 75 | +console.log(indexes); // [0, 1, 2] |
| 76 | +``` |
| 77 | + |
| 78 | +#### `asyncReduce()` |
| 79 | + |
| 80 | +Executes a reducer asynchronous function (that you provide) on each element of the array, resulting in a single output value. |
| 81 | + |
| 82 | +##### Sample usage |
| 83 | + |
| 84 | +```js |
| 85 | +import { asyncReduce } from '@wojtekmaj/async-array-utils'; |
| 86 | + |
| 87 | +const result = await asyncReduce( |
| 88 | + [1, 2, 3], |
| 89 | + async (tmp, cur, idx) => { return tmp + cur }, |
| 90 | + 0 |
| 91 | +); // 6 |
| 92 | +``` |
| 93 | + |
| 94 | +## License |
| 95 | + |
| 96 | +The MIT License. |
| 97 | + |
| 98 | +## Author |
| 99 | + |
| 100 | +<table> |
| 101 | + <tr> |
| 102 | + <td> |
| 103 | + <img src="https://github.com/wojtekmaj.png?s=100" width="100"> |
| 104 | + </td> |
| 105 | + <td> |
| 106 | + Wojciech Maj<br /> |
| 107 | + <a href="mailto:kontakt@wojtekmaj.pl">kontakt@wojtekmaj.pl</a><br /> |
| 108 | + <a href="http://wojtekmaj.pl">http://wojtekmaj.pl</a> |
| 109 | + </td> |
| 110 | + </tr> |
| 111 | +</table> |
0 commit comments