|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# parallel |
| 22 | + |
| 23 | +> Execute functions in parallel and pass the results of all functions to a provided callback. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var parallel = require( '@stdlib/utils/async/parallel' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### parallel( fcns, \[options,] done ) |
| 44 | + |
| 45 | +Executes a set of functions in parallel and passes the results of all functions to a provided callback. |
| 46 | + |
| 47 | +```javascript |
| 48 | +function a( clbk ) { |
| 49 | + setTimeout( onTimeout, 0 ); |
| 50 | + function onTimeout() { |
| 51 | + clbk( null, 2 ); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function b( clbk ) { |
| 56 | + setTimeout( onTimeout, 0 ); |
| 57 | + function onTimeout() { |
| 58 | + clbk( null, 4 ); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function done( error, out ) { |
| 63 | + if ( error ) { |
| 64 | + throw error; |
| 65 | + } |
| 66 | + console.log( out ); |
| 67 | + // => [ 2, 4 ] |
| 68 | +} |
| 69 | + |
| 70 | +var fcns = [ a, b ]; |
| 71 | + |
| 72 | +parallel( fcns, done ); |
| 73 | +``` |
| 74 | + |
| 75 | +The function accepts the following `options`: |
| 76 | + |
| 77 | +- **limit**: maximum number of functions to execute concurrently. Default: `infinity`. |
| 78 | +- **thisArg**: execution context for each function. |
| 79 | + |
| 80 | +To limit the maximum number of functions executing in parallel, set the `limit` option. |
| 81 | + |
| 82 | +```javascript |
| 83 | +function a( clbk ) { |
| 84 | + setTimeout( onTimeout, 0 ); |
| 85 | + function onTimeout() { |
| 86 | + clbk( null, 2 ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function b( clbk ) { |
| 91 | + setTimeout( onTimeout, 0 ); |
| 92 | + function onTimeout() { |
| 93 | + clbk( null, 4 ); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function done( error, out ) { |
| 98 | + if ( error ) { |
| 99 | + throw error; |
| 100 | + } |
| 101 | + console.log( out ); |
| 102 | + // => [ 2, 4 ] |
| 103 | +} |
| 104 | + |
| 105 | +var fcns = [ a, b ]; |
| 106 | + |
| 107 | +var opts = { |
| 108 | + 'limit': 1 |
| 109 | +}; |
| 110 | + |
| 111 | +parallel( fcns, opts, done ); |
| 112 | +``` |
| 113 | + |
| 114 | +To set the `this` context for **all** `functions` in the provided function array, set the `thisArg` option. |
| 115 | + |
| 116 | +```javascript |
| 117 | +function a( clbk ) { |
| 118 | + this.idx += 1; |
| 119 | + clbk( null, 2 ); |
| 120 | +} |
| 121 | + |
| 122 | +function b( clbk ) { |
| 123 | + this.idx += 1; |
| 124 | + clbk( null, 4 ); |
| 125 | +} |
| 126 | + |
| 127 | +var fcns = [ a, b ]; |
| 128 | +var ctx = { |
| 129 | + 'idx': 0 |
| 130 | +}; |
| 131 | +var opts = { |
| 132 | + 'thisArg': ctx |
| 133 | +}; |
| 134 | + |
| 135 | +parallel( fcns, opts, done ); |
| 136 | + |
| 137 | +function done( error, out ) { |
| 138 | + if ( error ) { |
| 139 | + throw error; |
| 140 | + } |
| 141 | + console.log( ctx.idx ); |
| 142 | + // => 2 |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +#### parallel.factory( fcns, \[options] ) |
| 147 | + |
| 148 | +Returns a reusable function which executes a set of functions in parallel. |
| 149 | + |
| 150 | +```javascript |
| 151 | +function a( clbk ) { |
| 152 | + setTimeout( onTimeout, 0 ); |
| 153 | + function onTimeout() { |
| 154 | + clbk( null, 2 ); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +function b( clbk ) { |
| 159 | + setTimeout( onTimeout, 0 ); |
| 160 | + function onTimeout() { |
| 161 | + clbk( null, 4 ); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +function done( error, out ) { |
| 166 | + if ( error ) { |
| 167 | + throw error; |
| 168 | + } |
| 169 | + console.log( out ); |
| 170 | + // => [ 2, 4 ] |
| 171 | +} |
| 172 | + |
| 173 | +var fcns = [ a, b ]; |
| 174 | + |
| 175 | +var run = parallel.factory( fcns ); |
| 176 | + |
| 177 | +run( done ); |
| 178 | +run( done ); |
| 179 | +run( done ); |
| 180 | +``` |
| 181 | + |
| 182 | +</section> |
| 183 | + |
| 184 | +<!-- /.usage --> |
| 185 | + |
| 186 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 187 | + |
| 188 | +<section class="notes"> |
| 189 | + |
| 190 | +## Notes |
| 191 | + |
| 192 | +- The order of the results provided to the `done` callback corresponds to the order of the provided functions. |
| 193 | +- When executed, each provided function is invoked with a single callback argument. The callback should be invoked upon function completion. The first argument is reserved as an `error` argument (which can be `null`). The second argument is reserved for any results which should be passed to the `done` callback upon completion of all provided functions. |
| 194 | +- If any function fails to invoke the callback argument, the `done` callback will never be invoked. |
| 195 | +- This implementation is intended to start asynchronous tasks so that execution of each task runs concurrently. If provided a function which does not perform asynchronous tasks, the function will execute synchronously. Hence, this implementation does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a `function` which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). |
| 196 | +- The function executes provided functions in the same thread. Accordingly, the function does **not** spawn new threads. |
| 197 | + |
| 198 | +</section> |
| 199 | + |
| 200 | +<!-- /.notes --> |
| 201 | + |
| 202 | +<!-- Package usage examples. --> |
| 203 | + |
| 204 | +<section class="examples"> |
| 205 | + |
| 206 | +## Examples |
| 207 | + |
| 208 | +<!-- eslint no-undef: "error" --> |
| 209 | + |
| 210 | +```javascript |
| 211 | +var parallel = require( '@stdlib/utils/async/parallel' ); |
| 212 | + |
| 213 | +function foo( clbk ) { |
| 214 | + setTimeout( onTimeout, 300 ); |
| 215 | + function onTimeout() { |
| 216 | + clbk( null, 'one' ); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +function bar( clbk ) { |
| 221 | + setTimeout( onTimeout, 100 ); |
| 222 | + function onTimeout() { |
| 223 | + clbk( null, 'two' ); |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +function done( error, results ) { |
| 228 | + if ( error ) { |
| 229 | + throw error; |
| 230 | + } |
| 231 | + console.log( results ); |
| 232 | + // => [ 'one', 'two' ] |
| 233 | +} |
| 234 | + |
| 235 | +var fcns = [ foo, bar ]; |
| 236 | + |
| 237 | +parallel( fcns, done ); |
| 238 | +``` |
| 239 | + |
| 240 | +</section> |
| 241 | + |
| 242 | +<!-- /.examples --> |
| 243 | + |
| 244 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 245 | + |
| 246 | +<section class="references"> |
| 247 | + |
| 248 | +</section> |
| 249 | + |
| 250 | +<!-- /.references --> |
| 251 | + |
| 252 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 253 | + |
| 254 | +<section class="related"> |
| 255 | + |
| 256 | +</section> |
| 257 | + |
| 258 | +<!-- /.related --> |
| 259 | + |
| 260 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 261 | + |
| 262 | +<section class="links"> |
| 263 | + |
| 264 | +</section> |
| 265 | + |
| 266 | +<!-- /.links --> |
0 commit comments