repl.txt (1554B)
1 2 {{alias}}( predicate, x, y, done ) 3 If a predicate function returns a truthy value, returns `x`; otherwise, 4 returns `y`. 5 6 A predicate function is provided a single argument: 7 8 - clbk: callback to invoke upon predicate completion 9 10 The callback function accepts two arguments: 11 12 - error: error object 13 - bool: condition used to determine whether to invoke `x` or `y` 14 15 The `done` callback is invoked upon function completion and is provided at 16 most two arguments: 17 18 - error: error object 19 - result: either `x` or `y` 20 21 Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, 22 wrap the `done` callback in a function which either executes at the end of 23 the current stack (e.g., `nextTick`) or during a subsequent turn of the 24 event loop (e.g., `setImmediate`, `setTimeout`). 25 26 Parameters 27 ---------- 28 predicate: Function 29 Predicate function. 30 31 x: any 32 Value to return if a condition is truthy. 33 34 y: any 35 Value to return if a condition is falsy. 36 37 done: Function 38 Callback to invoke upon completion. 39 40 Examples 41 -------- 42 > function predicate( clbk ) { 43 ... setTimeout( onTimeout, 0 ); 44 ... function onTimeout() { 45 ... clbk( null, true ); 46 ... } 47 ... }; 48 > function done( error, result ) { 49 ... if ( error ) { 50 ... throw error; 51 ... } 52 ... console.log( result ); 53 ... }; 54 > {{alias}}( predicate, 'beep', 'boop', done ) 55 'beep' 56 57 See Also 58 -------- 59