repl.txt (4218B)
1 2 {{alias}}( fcn, n, [options,] done ) 3 Invokes a function `n` times and returns an array of accumulated function 4 return values. 5 6 For each iteration, the provided function is invoked with two arguments: 7 8 - `index`: invocation index (starting from zero) 9 - `next`: callback to be invoked upon function completion 10 11 The `next` callback accepts two arguments: 12 13 - `error`: error argument 14 - `result`: function result 15 16 If a provided function calls the `next` callback with a truthy `error` 17 argument, the function suspends execution and immediately calls the `done` 18 callback for subsequent `error` handling. 19 20 Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, 21 wrap the `done` callback in a function which either executes at the end of 22 the current stack (e.g., `nextTick`) or during a subsequent turn of the 23 event loop (e.g., `setImmediate`, `setTimeout`). 24 25 Parameters 26 ---------- 27 fcn: Function 28 Function to invoke. 29 30 n: integer 31 Number of times to invoke a function. 32 33 options: Object (optional) 34 Function options. 35 36 options.limit: integer (optional) 37 Maximum number of pending invocations. Default: Infinity. 38 39 options.series: boolean (optional) 40 Boolean indicating whether to allow only one pending invocation at a 41 time. Default: false. 42 43 options.thisArg: any (optional) 44 Execution context. 45 46 done: Function 47 A callback invoked upon executing a provided function `n` times or upon 48 encountering an error. 49 50 Examples 51 -------- 52 // Basic usage: 53 > function fcn( i, next ) { 54 ... setTimeout( onTimeout, 0 ); 55 ... function onTimeout() { 56 ... next( null, i ); 57 ... } 58 ... }; 59 > function done( error, arr ) { 60 ... if ( error ) { 61 ... throw error; 62 ... } 63 ... console.log( arr ); 64 ... }; 65 > {{alias}}( fcn, 10, done ) 66 [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] 67 68 // Limit number of concurrent invocations: 69 > function fcn( i, next ) { 70 ... setTimeout( onTimeout, 0 ); 71 ... function onTimeout() { 72 ... next( null, i ); 73 ... } 74 ... }; 75 > function done( error, arr ) { 76 ... if ( error ) { 77 ... throw error; 78 ... } 79 ... console.log( arr ); 80 ... }; 81 > var opts = { 'limit': 2 }; 82 > {{alias}}( fcn, 10, opts, done ) 83 [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] 84 85 // Sequential invocation: 86 > function fcn( i, next ) { 87 ... setTimeout( onTimeout, 0 ); 88 ... function onTimeout() { 89 ... next( null, i ); 90 ... } 91 ... }; 92 > function done( error, arr ) { 93 ... if ( error ) { 94 ... throw error; 95 ... } 96 ... console.log( arr ); 97 ... }; 98 > var opts = { 'series': true }; 99 > {{alias}}( fcn, 10, opts, done ) 100 [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] 101 102 103 {{alias}}.factory( [options,] fcn ) 104 Returns a function which invokes a function `n` times and returns an array 105 of accumulated function return values. 106 107 Parameters 108 ---------- 109 options: Object (optional) 110 Function options. 111 112 options.limit: integer (optional) 113 Maximum number of pending invocations. Default: Infinity. 114 115 options.series: boolean (optional) 116 Boolean indicating whether to allow only one pending invocation at a 117 time. Default: false. 118 119 options.thisArg: any (optional) 120 Execution context. 121 122 fcn: Function 123 Function to invoke. 124 125 Returns 126 ------- 127 out: Function 128 A function which invokes a function `n` times and returns an array of 129 accumulated function return values. 130 131 Examples 132 -------- 133 > function fcn( i, next ) { 134 ... setTimeout( onTimeout, 0 ); 135 ... function onTimeout() { 136 ... next( null, i ); 137 ... } 138 ... }; 139 > var opts = { 'series': true }; 140 > var f = {{alias}}.factory( opts, fcn ); 141 > function done( error, arr ) { 142 ... if ( error ) { 143 ... throw error; 144 ... } 145 ... console.log( arr ); 146 ... }; 147 > f( 10, done ) 148 [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] 149 150 See Also 151 -------- 152