repl.txt (2223B)
1 2 {{alias}}( predicate, x, y, done ) 3 If a predicate function returns a truthy value, invokes `x`; otherwise, 4 invokes `y`. 5 6 The predicate function is provided a single argument: 7 8 - clbk: callback to invoke upon predicate function completion 9 10 The predicate function callback accepts two arguments: 11 12 - error: error object 13 - bool: condition used to determine whether to invoke `x` or `y` 14 15 Both `x` and `y` are provided a single argument: 16 17 - clbk: callback to invoke upon function completion 18 19 The callback function accepts any number of arguments, with the first 20 argument reserved for providing an error. 21 22 If the error argument is falsy, the `done` callback is invoked with its 23 first argument as `null` and all other provided arguments. 24 25 If the error argument is truthy, the `done` callback is invoked with only an 26 error argument. 27 28 Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, 29 wrap the `done` callback in a function which either executes at the end of 30 the current stack (e.g., `nextTick`) or during a subsequent turn of the 31 event loop (e.g., `setImmediate`, `setTimeout`). 32 33 Parameters 34 ---------- 35 predicate: Function 36 Predicate function. 37 38 x: Function 39 Function to invoke if a condition is truthy. 40 41 y: Function 42 Function to invoke if a condition is falsy. 43 44 done: Function 45 Callback to invoke upon completion. 46 47 Examples 48 -------- 49 > function predicate( clbk ) { 50 ... setTimeout( onTimeout, 0 ); 51 ... function onTimeout() { 52 ... clbk( null, false ); 53 ... } 54 ... }; 55 > function x( clbk ) { 56 ... setTimeout( onTimeout, 0 ); 57 ... function onTimeout() { 58 ... clbk( null, 'beep' ); 59 ... } 60 ... }; 61 > function y( clbk ) { 62 ... setTimeout( onTimeout, 0 ); 63 ... function onTimeout() { 64 ... clbk( null, 'boop' ); 65 ... } 66 ... }; 67 > function done( error, result ) { 68 ... if ( error ) { 69 ... throw error; 70 ... } 71 ... console.log( result ); 72 ... }; 73 > {{alias}}( predicate, x, y, done ) 74 'boop' 75 76 See Also 77 -------- 78