README.md (4105B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # trycatchAsync 22 23 > If a function does not return an error, invoke a callback with the function result; otherwise, invoke a callback with a value `y`. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var trycatchAsync = require( '@stdlib/utils/async/try-catch' ); 41 ``` 42 43 #### trycatchAsync( x, y, done ) 44 45 If a function `x` does not return an error, invokes a `done` callback with the function result; otherwise, invokes a `done` callback with a value `y`. 46 47 ```javascript 48 var randu = require( '@stdlib/random/base/randu' ); 49 50 function x( clbk ) { 51 setTimeout( onTimeout, 0 ); 52 function onTimeout() { 53 if ( randu() > 0.5 ) { 54 return clbk( null, 1.0 ); 55 } 56 clbk( new Error( 'oops' ) ); 57 } 58 } 59 60 function done( error, result ) { 61 if ( error ) { 62 console.log( error.message ); 63 } 64 console.log( result ); 65 } 66 67 trycatchAsync( x, -1.0, done ); 68 ``` 69 70 The function `x` is provided a single argument: 71 72 - `clbk`: callback to invoke upon function completion 73 74 The callback accepts two arguments: 75 76 - `error`: error object 77 - `result`: function result 78 79 The `done` callback is invoked upon function completion and is provided two arguments: 80 81 - `error`: error object 82 - `result`: either the result of `x` or the provided value `y` 83 84 If the function `x` does not return a truthy `error` argument, the `error` argument provided to the `done` callback is `null`. If `x` does return a truthy `error` argument, the `done` callback is invoked with both the `error` and the provided value `y`. 85 86 </section> 87 88 <!-- /.usage --> 89 90 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 91 92 <section class="notes"> 93 94 ## Notes 95 96 - Execution is **not** guaranteed to be asynchronous. To guarantee asynchrony, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). 97 98 </section> 99 100 <!-- /.notes --> 101 102 <!-- Package usage examples. --> 103 104 <section class="examples"> 105 106 ## Examples 107 108 <!-- eslint no-undef: "error" --> 109 110 ```javascript 111 var randu = require( '@stdlib/random/base/randu' ); 112 var trycatchAsync = require( '@stdlib/utils/async/try-catch' ); 113 114 var i; 115 116 function next() { 117 trycatchAsync( x, 'beep', done ); 118 } 119 120 function x( clbk ) { 121 setTimeout( onTimeout, 0 ); 122 function onTimeout() { 123 if ( randu() > 0.9 ) { 124 return clbk( null, 'BOOP' ); 125 } 126 clbk( new Error( 'oops' ) ); 127 } 128 } 129 130 function done( error, result ) { 131 if ( error ) { 132 console.log( error.message ); 133 } 134 i += 1; 135 console.log( result ); 136 if ( i < 100 ) { 137 return next(); 138 } 139 } 140 141 i = 0; 142 next(); 143 ``` 144 145 </section> 146 147 <!-- /.examples --> 148 149 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 150 151 <section class="references"> 152 153 </section> 154 155 <!-- /.references --> 156 157 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 158 159 <section class="links"> 160 161 </section> 162 163 <!-- /.links -->