repl.txt (5912B)
1 2 {{alias}}( collection, [options,] fcn, done ) 3 Invokes a function once for each element in a collection and updates a 4 collection in-place. 5 6 When invoked, `fcn` is provided a maximum of four arguments: 7 8 - `value`: collection value 9 - `index`: collection index 10 - `collection`: the input collection 11 - `next`: a callback to be invoked after processing a collection `value` 12 13 The actual number of provided arguments depends on function length. If `fcn` 14 accepts two arguments, `fcn` is provided: 15 16 - `value` 17 - `next` 18 19 If `fcn` accepts three arguments, `fcn` is provided: 20 21 - `value` 22 - `index` 23 - `next` 24 25 For every other `fcn` signature, `fcn` is provided all four arguments. 26 27 The `next` callback accepts two arguments: 28 29 - `error`: error argument 30 - `result`: value used to update the collection 31 32 If a provided function calls the `next` callback with a truthy `error` 33 argument, the function suspends execution and immediately calls the `done` 34 callback for subsequent `error` handling. Note, however, that the function 35 may have mutated an input collection during prior invocations, resulting in 36 a partially mutated collection. 37 38 Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, 39 wrap the `done` callback in a function which either executes at the end of 40 the current stack (e.g., `nextTick`) or during a subsequent turn of the 41 event loop (e.g., `setImmediate`, `setTimeout`). 42 43 The function does not support dynamic collection resizing. 44 45 The function does not skip `undefined` elements. 46 47 Parameters 48 ---------- 49 collection: Array|TypedArray|Object 50 Input collection over which to iterate. If provided an object, the 51 object must be array-like (excluding strings and functions). 52 53 options: Object (optional) 54 Function options. 55 56 options.limit: integer (optional) 57 Maximum number of pending invocations. Default: Infinity. 58 59 options.series: boolean (optional) 60 Boolean indicating whether to process each collection element 61 sequentially. Default: false. 62 63 options.thisArg: any (optional) 64 Execution context. 65 66 fcn: Function 67 The function to invoke for each element in a collection. 68 69 done: Function 70 A callback invoked either upon processing all collection elements or 71 upon encountering an error. 72 73 Examples 74 -------- 75 // Basic usage: 76 > function fcn( value, index, next ) { 77 ... setTimeout( onTimeout, value ); 78 ... function onTimeout() { 79 ... console.log( value ); 80 ... next( null, value*index ); 81 ... } 82 ... }; 83 > function done( error, collection ) { 84 ... if ( error ) { 85 ... throw error; 86 ... } 87 ... console.log( collection === arr ); 88 ... console.log( collection ); 89 ... }; 90 > var arr = [ 3000, 2500, 1000 ]; 91 > {{alias}}( arr, fcn, done ) 92 1000 93 2500 94 3000 95 true 96 [ 0, 2500, 2000 ] 97 98 // Limit number of concurrent invocations: 99 > function fcn( value, index, next ) { 100 ... setTimeout( onTimeout, value ); 101 ... function onTimeout() { 102 ... console.log( value ); 103 ... next( null, value*index ); 104 ... } 105 ... }; 106 > function done( error, collection ) { 107 ... if ( error ) { 108 ... throw error; 109 ... } 110 ... console.log( collection === arr ); 111 ... console.log( collection ); 112 ... }; 113 > var opts = { 'limit': 2 }; 114 > var arr = [ 3000, 2500, 1000 ]; 115 > {{alias}}( arr, opts, fcn, done ) 116 2500 117 3000 118 1000 119 true 120 [ 0, 2500, 2000 ] 121 122 // Process sequentially: 123 > function fcn( value, index, next ) { 124 ... setTimeout( onTimeout, value ); 125 ... function onTimeout() { 126 ... console.log( value ); 127 ... next( null, value*index ); 128 ... } 129 ... }; 130 > function done( error, collection ) { 131 ... if ( error ) { 132 ... throw error; 133 ... } 134 ... console.log( collection === arr ); 135 ... console.log( collection ); 136 ... }; 137 > var opts = { 'series': true }; 138 > var arr = [ 3000, 2500, 1000 ]; 139 > {{alias}}( arr, opts, fcn, done ) 140 3000 141 2500 142 1000 143 true 144 [ 0, 2500, 2000 ] 145 146 147 {{alias}}.factory( [options,] fcn ) 148 Returns a function which invokes a function once for each element in a 149 collection and updates a collection in-place. 150 151 Parameters 152 ---------- 153 options: Object (optional) 154 Function options. 155 156 options.limit: integer (optional) 157 Maximum number of pending invocations. Default: Infinity. 158 159 options.series: boolean (optional) 160 Boolean indicating whether to process each collection element 161 sequentially. Default: false. 162 163 options.thisArg: any (optional) 164 Execution context. 165 166 fcn: Function 167 The function to invoke for each element in a collection. 168 169 Returns 170 ------- 171 out: Function 172 A function which invokes a function for each element in a collection. 173 174 Examples 175 -------- 176 > function fcn( value, index, next ) { 177 ... setTimeout( onTimeout, value ); 178 ... function onTimeout() { 179 ... console.log( value ); 180 ... next( null, value*index ); 181 ... } 182 ... }; 183 > var opts = { 'series': true }; 184 > var f = {{alias}}.factory( opts, fcn ); 185 > function done( error, collection ) { 186 ... if ( error ) { 187 ... throw error; 188 ... } 189 ... console.log( collection === arr ); 190 ... console.log( collection ); 191 ... }; 192 > var arr = [ 3000, 2500, 1000 ]; 193 > f( arr, done ) 194 3000 195 2500 196 1000 197 true 198 [ 0, 2500, 2000 ] 199 > arr = [ 2000, 1500, 1000 ]; 200 > f( arr, done ) 201 2000 202 1500 203 1000 204 true 205 [ 0, 1500, 2000 ] 206 207 See Also 208 -------- 209