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