|
919 | 919 | <div class="tsd-panel tsd-typography">
|
920 | 920 | <h1 id="async-parallel-foreach">Async parallel forEach</h1>
|
921 | 921 | <p><a href="https://www.npmjs.com/package/async-parallel-foreach"><img src="https://img.shields.io/npm/v/async-parallel-foreach.svg" alt="npm"></a>
|
922 |
| - <a href="https://www.npmjs.com/package/async-parallel-foreach"><img src="./coverage/badge.svg" alt="npm"></a> |
923 |
| - <a href="https://www.npmjs.com/package/async-parallel-foreach"><img src="https://img.shields.io/npm/l/async-parallel-foreach.svg" alt="npm"></a></p> |
924 |
| - <p>Javascript module to perform <strong><em>async flow control</em></strong> on collection/iterable/dictionary <strong><em>in parallel</em></strong> and make <strong><em>retry easily</em></strong> when error occurred</p> |
| 922 | + <a href="https://github.com/Donaldcwl/async-parallel-foreach"><img src="./coverage/badge.svg" alt="npm"></a> |
| 923 | + <a href="https://github.com/Donaldcwl/async-parallel-foreach"><img src="https://img.shields.io/npm/l/async-parallel-foreach.svg" alt="npm"></a></p> |
| 924 | + <p>Javascript module to perform <strong><em>async flow control</em></strong> on collection/iterable/dictionary <strong><em>in controlled parallel</em></strong> and make <strong><em>retry easily</em></strong> when error occurred</p> |
| 925 | + <h2 id="features">Features</h2> |
| 926 | + <ul> |
| 927 | + <li>iterate collection (array/object/iterator) and <strong><em>run async function on each item</em></strong> in a collection</li> |
| 928 | + <li>control the <strong><em>concurrency</em></strong> of running async function on the items</li> |
| 929 | + <li><strong><em>auto retry</em></strong> when error occurred</li> |
| 930 | + <li><strong><em>delayed retry</em></strong></li> |
| 931 | + </ul> |
925 | 932 | <h2 id="install">Install</h2>
|
926 |
| - <pre><code>npm install <span class="hljs-keyword">async</span>-<span class="hljs-keyword">parallel</span>-foreach --save |
927 |
| -<span class="hljs-keyword">or</span> |
928 |
| -yarn <span class="hljs-keyword">add</span> <span class="hljs-keyword">async</span>-<span class="hljs-keyword">parallel</span>-foreach</code></pre><h2 id="how-to-use-this-module-in-your-project-">How to use this module in your project?</h2> |
| 933 | + <pre><code class="language-bash">npm install async-parallel-foreach async --save |
| 934 | +or |
| 935 | +yarn add async-parallel-foreach async</code></pre> |
| 936 | + <h2 id="how-to-use-this-module-in-your-project-">How to use this module in your project?</h2> |
929 | 937 | <p>Frontend: used in framework like React, Angular, Vue etc
|
930 | 938 | (work with bundler like webpack and rollup)</p>
|
931 |
| - <p>Backend: node.js</p> |
932 | 939 | <pre><code class="language-javascript"><span class="hljs-keyword">import</span> { asyncParallelForEach, BACK_OFF_RETRY } <span class="hljs-keyword">from</span> <span class="hljs-string">'async-parallel-foreach'</span></code></pre>
|
| 940 | + <p>Backend: node.js</p> |
| 941 | + <pre><code class="language-javascript"><span class="hljs-keyword">const</span> { asyncParallelForEach, BACK_OFF_RETRY } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'async-parallel-foreach'</span>)</code></pre> |
933 | 942 | <h2 id="api">API</h2>
|
934 | 943 | <h3 id="main-function">Main function</h3>
|
935 | 944 | <h4 id="asyncparallelforeach-coll-collection-parallellimit-number-iteratee-function-eachmaxtry-promise-array-value-any-error-error-">asyncParallelForEach(coll: Collection, parallelLimit: number, iteratee: Function, eachMaxTry): Promise<Array<{ value: any, error: Error }>></h4>
|
936 | 945 | <ul>
|
937 | 946 | <li>coll - can be Array, Object (dictionary), Iterable</li>
|
938 |
| - <li>parallelLimit - number of iteratee functions to be executed in parallel at any time</li> |
939 |
| - <li>iteratee - the function that you define to process each item in "coll"</li> |
| 947 | + <li>parallelLimit - number of iteratee functions to be executed in parallel at any time, set <code>parallelLimit = -1</code> for unlimited parallelization (all items will start process at once)</li> |
| 948 | + <li>iteratee - the function that you define to process each item in "coll"<ul> |
| 949 | + <li>if "coll" is array, it will call with (value, index) </li> |
| 950 | + <li>if "coll" is object, it will call with (value, key)</li> |
| 951 | + </ul> |
| 952 | + </li> |
940 | 953 | <li>eachMaxTry - maximum number of times each item will be processed by "iteratee".<ul>
|
941 |
| - <li>if eachMaxTry = 2, then the item will be retried 1 time when there is error throwed in the iteratee function</li> |
| 954 | + <li>if <code>eachMaxTry = 2</code>, then the item will be retried 1 time when there is error throwed in the iteratee function</li> |
942 | 955 | <li>add delay before retry<ul>
|
943 |
| - <li>set eachMaxTry = { times: 2, interval: 1000 } // wait for 1000 ms before retry</li> |
| 956 | + <li>set <code>eachMaxTry = { times: 2, interval: 1000 }</code> // wait for 1000 ms before retry</li> |
944 | 957 | <li>interval can also accept function returning the interval in ms<ul>
|
945 |
| - <li>e.g. eachMaxTry = { times: 2, interval: (retryCount) => retryCount * 1000 } // retryCount start from 2 which means it is the 2nd trial</li> |
| 958 | + <li>e.g. <code>eachMaxTry = { times: 2, interval: (retryCount) => retryCount * 1000 }</code> // retryCount start from 2 which means it is the 2nd trial<h3 id="back_off_retry-strategies">BACK_OFF_RETRY strategies</h3> |
| 959 | + </li> |
946 | 960 | </ul>
|
947 | 961 | </li>
|
948 | 962 | </ul>
|
949 | 963 | </li>
|
950 |
| - <li>eachMaxTry follows the "opts" argument in <a href="https://caolan.github.io/async/docs.html#retry">https://caolan.github.io/async/docs.html#retry</a> "retry" <h3 id="back_off_retry-strategies">BACK_OFF_RETRY strategies</h3> |
951 |
| - </li> |
952 | 964 | </ul>
|
953 | 965 | </li>
|
954 | 966 | <li>predefined interval function you may use<h4 id="back_off_retry-randombetween-minms-number-maxms-number-">BACK_OFF_RETRY.randomBetween(minMs: number, maxMs: number)</h4>
|
955 |
| - <h4 id="back_off_retry-exponential-">BACK_OFF_RETRY.exponential()</h4> |
| 967 | + </li> |
| 968 | + <li>e.g. <code>eachMaxTry = { times: 5, interval: BACK_OFF_RETRY.randomBetween(100, 3000) }</code> // random delay between 100ms and 3000ms<h4 id="back_off_retry-exponential-">BACK_OFF_RETRY.exponential()</h4> |
956 | 969 | </li>
|
957 | 970 | <li>start from 100ms, then 200ms, 400ms, 800ms, 1600ms, ...</li>
|
958 |
| - <li>e.g. eachMaxTry = { times: 5, interval: BACK_OFF_RETRY.randomBetween(100, 3000) } // random delay between 100ms and 3000ms</li> |
959 | 971 | </ul>
|
960 | 972 | <p>(<a href="http://htmlpreview.github.io/?https://github.com/Donaldcwl/async-parallel-foreach/blob/master/docs/index.html">details api document in here</a>)</p>
|
961 | 973 | <h2 id="usage">Usage</h2>
|
@@ -1026,7 +1038,21 @@ <h2 id="usage">Usage</h2>
|
1026 | 1038 | // }
|
1027 | 1039 |
|
1028 | 1040 | <span class="hljs-keyword">return</span> results
|
1029 |
| -}</code></pre> |
| 1041 | +}</code></pre><h2 id="example">Example</h2> |
| 1042 | + <p>Please check the "example" folder in this repo</p> |
| 1043 | + <ul> |
| 1044 | + <li>How to run the example:<pre><code class="language-bash">git <span class="hljs-built_in">clone</span> https://github.com/Donaldcwl/async-parallel-foreach.git |
| 1045 | +<span class="hljs-built_in">cd</span> async-parallel-foreach/example |
| 1046 | +yarn install <span class="hljs-comment"># or npm install</span> |
| 1047 | +node example.js</code></pre> |
| 1048 | + </li> |
| 1049 | + </ul> |
| 1050 | + <h3 id="todo-features">TODO FEATURES</h3> |
| 1051 | + <ul> |
| 1052 | + <li>get current status in the iteratee function e.g. currentTrial, isFirstTrial, isLastTrial, timeElapsed, failedReasons, incrementMaxTry</li> |
| 1053 | + <li>eachTrialTimeout, eachItemTimeout</li> |
| 1054 | + <li>run iteratee function in web worker for CPU intensive tasks (use tiny-worker for node.js)</li> |
| 1055 | + </ul> |
1030 | 1056 | </div>
|
1031 | 1057 | <div style="position:relative;"><a name="typedoc-main-index" class="tsd-anchor"></a></div>
|
1032 | 1058 | <section class="tsd-panel-group tsd-index-group">
|
@@ -1060,7 +1086,7 @@ <h3>async<wbr>Parallel<wbr>For<wbr>Each</h3>
|
1060 | 1086 | <li class="tsd-description">
|
1061 | 1087 | <aside class="tsd-sources">
|
1062 | 1088 | <ul>
|
1063 |
| - <li>Defined in <a href="https://github.com/Donaldcwl/async-parallel-foreach/blob/358bd6c/src/async-parallel-foreach.ts#L3">async-parallel-foreach.ts:3</a></li> |
| 1089 | + <li>Defined in <a href="https://github.com/Donaldcwl/async-parallel-foreach/blob/41a4e78/src/async-parallel-foreach.ts#L3">async-parallel-foreach.ts:3</a></li> |
1064 | 1090 | </ul>
|
1065 | 1091 | </aside>
|
1066 | 1092 | <h4 class="tsd-type-parameters-title">Type parameters</h4>
|
|
0 commit comments