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