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