README.md (3269B)
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 # trythen 22 23 > If a function does not throw, return the function return value; otherwise, return the return value of a second function. 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 trythen = require( '@stdlib/utils/try-then' ); 41 ``` 42 43 #### trythen( x, y ) 44 45 If a function `x` does not throw, returns the return value of `x`; otherwise, returns the return value of a second function `y`. 46 47 ```javascript 48 function x1() { 49 return 1.0; 50 } 51 52 function x2() { 53 throw new Error( 'beep' ); 54 } 55 56 function y() { 57 return -1.0; 58 } 59 60 var z = trythen( x1, y ); 61 // returns 1.0 62 63 z = trythen( x2, y ); 64 // returns -1.0 65 ``` 66 67 The function `y` is provided a single argument: 68 69 - `error`: the error thrown by `x` 70 71 ```javascript 72 var randu = require( '@stdlib/random/base/randu' ); 73 74 function x() { 75 if ( randu() < 0.5 ) { 76 throw new Error( 'beep' ); 77 } 78 throw new TypeError( 'boop' ); 79 } 80 81 function y( err ) { 82 if ( err instanceof TypeError ) { 83 return 'boops'; 84 } 85 return 'beeps'; 86 } 87 88 var z = trythen( x, y ); 89 // returns <string> 90 ``` 91 92 </section> 93 94 <!-- /.usage --> 95 96 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 97 98 <section class="notes"> 99 100 </section> 101 102 <!-- /.notes --> 103 104 <!-- Package usage examples. --> 105 106 <section class="examples"> 107 108 ## Examples 109 110 <!-- eslint no-undef: "error" --> 111 112 ```javascript 113 var randu = require( '@stdlib/random/base/randu' ); 114 var ceil = require( '@stdlib/math/base/special/ceil' ); 115 var repeatString = require( '@stdlib/string/repeat' ); 116 var trythen = require( '@stdlib/utils/try-then' ); 117 118 var z; 119 var i; 120 121 function x() { 122 if ( randu() < 0.9 ) { 123 throw new Error( 'BOOP' ); 124 } 125 return repeatString( 'BOOP', ceil( randu()*3.0 ) ); 126 } 127 128 function y() { 129 return repeatString( 'beep', ceil( randu()*5.0 ) ); 130 } 131 132 for ( i = 0; i < 100; i++ ) { 133 z = trythen( x, y ); 134 console.log( z ); 135 } 136 ``` 137 138 </section> 139 140 <!-- /.examples --> 141 142 <!-- 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. --> 143 144 <section class="references"> 145 146 </section> 147 148 <!-- /.references --> 149 150 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 151 152 <section class="links"> 153 154 </section> 155 156 <!-- /.links -->