repl.txt (1806B)
1 2 {{alias}}( x, y, done ) 3 If a function does not return an error, invokes a callback with the function 4 result; otherwise, invokes a callback with a value `y`. 5 6 A function `x` is provided a single argument: 7 8 - clbk: callback to invoke upon function completion 9 10 The callback function accepts two arguments: 11 12 - error: error object 13 - result: function result 14 15 The `done` callback is invoked upon function completion and is provided two 16 arguments: 17 18 - error: error object 19 - result: either the result of `x` or the provided argument `y` 20 21 If `x` invokes `clbk` with an error argument, the function invokes the 22 `done` callback with both the error and the argument `y`. 23 24 If `x` does not invoke `clbk` with an error argument, the function invokes 25 the `done` callback with a first argument equal to `null` and the function 26 `result`. 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 x: Function 36 Function to invoke. 37 38 y: any 39 Value to return if `x` returns an error. 40 41 done: Function 42 Callback to invoke upon completion. 43 44 Examples 45 -------- 46 > function x( clbk ) { 47 ... setTimeout( onTimeout, 0 ); 48 ... function onTimeout() { 49 ... clbk( new Error( 'beep' ) ); 50 ... } 51 ... }; 52 > function done( error, result ) { 53 ... if ( error ) { 54 ... // process error... 55 ... } 56 ... console.log( result ); 57 ... }; 58 > {{alias}}( x, 'boop', done ) 59 'boop' 60 61 See Also 62 -------- 63