repl.txt (2329B)
1 2 {{alias}}( fcn, predicate, done[, thisArg] ) 3 Invokes a function while a test condition is true. 4 5 The condition is evaluated *after* executing the provided function; thus, 6 `fcn` *always* executes at least once. 7 8 The function to invoke is provided two arguments: 9 10 - `i`: iteration number (starting from zero) 11 - `next`: a callback which must be invoked before proceeding to the next 12 iteration 13 14 The first argument of the `next` callback is an `error` argument. If `fcn` 15 calls the `next` callback with a truthy `error` argument, the function 16 suspends execution and immediately calls the `done` callback for subsequent 17 `error` handling. 18 19 The predicate function is provided two arguments: 20 21 - `i`: iteration number (starting from one) 22 - `clbk`: a callback indicating whether to invoke `fcn` 23 24 The `clbk` function accepts two arguments: 25 26 - `error`: error argument 27 - `bool`: test result 28 29 If the test result is truthy, the function continues invoking `fcn`; 30 otherwise, the function invokes the `done` callback. 31 32 The `done` callback is invoked with an `error` argument and any arguments 33 passed to the final `next` callback. 34 35 Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, 36 wrap the `done` callback in a function which either executes at the end of 37 the current stack (e.g., `nextTick`) or during a subsequent turn of the 38 event loop (e.g., `setImmediate`, `setTimeout`). 39 40 Parameters 41 ---------- 42 fcn: Function 43 The function to invoke. 44 45 predicate: Function 46 The predicate function which indicates whether to continue invoking a 47 function. 48 49 done: Function 50 Callback to invoke upon completion. 51 52 thisArg: any (optional) 53 Execution context for the invoked function. 54 55 Examples 56 -------- 57 > function fcn( i, next ) { 58 ... setTimeout( onTimeout, i ); 59 ... function onTimeout() { 60 ... next( null, 'boop'+i ); 61 ... } 62 ... }; 63 > function predicate( i, clbk ) { clbk( null, i < 5 ); }; 64 > function done( error, result ) { 65 ... if ( error ) { 66 ... throw error; 67 ... } 68 ... console.log( result ); 69 ... }; 70 > {{alias}}( fcn, predicate, done ) 71 boop: 4 72 73 See Also 74 -------- 75