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