README.md (3698B)
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 # Try Function 22 23 > Wrap a function in a try/catch block. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var wrap = require( '@stdlib/utils/try-function' ); 31 ``` 32 33 #### wrap( fcn ) 34 35 Wraps a `function` in a `try/catch` block. 36 37 ```javascript 38 function fcn() { 39 throw new Error( 'beep boop' ); 40 } 41 42 var f = wrap( fcn ); 43 44 var out = f(); 45 if ( out instanceof Error ) { 46 console.error( out.message ); 47 // => 'beep boop' 48 } 49 ``` 50 51 The returned `function` has the same signature as the wrapped `function`. 52 53 ```javascript 54 function fcn( a, b, c, d ) { 55 var sum = a + b + c + d; 56 if ( sum < 10 ) { 57 throw new Error( 'invalid arguments. Arguments must sum to a number greater than or equal to 10.' ); 58 } 59 return sum; 60 } 61 62 var f = wrap( fcn ); 63 64 var out = f( 5, 6, 7, 8 ); 65 // returns 26 66 67 out = f( 1, 2, 3, 1 ); 68 // returns <Error> 69 ``` 70 71 If provided an asynchronous `function`, the returned `function` only traps `errors` which occur during the current event loop tick. 72 73 <!-- run throws: true --> 74 75 ```javascript 76 function fcn( a, b, clbk ) { 77 if ( !a ) { 78 throw new Error( 'invalid argument.' ); 79 } 80 setTimeout( onTimeout, 0 ); 81 function onTimeout() { 82 if ( !b ) { 83 throw new Error( 'invalid argument.' ); 84 } 85 clbk(); 86 } 87 } 88 89 function done() { 90 console.log( 'beep' ); 91 } 92 93 var f = wrap( fcn ); 94 95 var out = f( null, 5, done ); 96 // returns <Error> 97 98 out = f( true, null, done ); 99 // returns undefined 100 ``` 101 102 </section> 103 104 <!-- /.usage --> 105 106 <section class="notes"> 107 108 ## Notes 109 110 - Isolating `try/catch` blocks as separate wrapped `functions` prevents a parent scope from permanently entering optimization hell. 111 - If a function throws a literal, the literal is serialized as a `string` and returned as an `Error` object. 112 113 </section> 114 115 <!-- /.notes --> 116 117 <section class="examples"> 118 119 ## Examples 120 121 <!-- run throws: true --> 122 123 <!-- eslint no-undef: "error" --> 124 125 ```javascript 126 var wrap = require( '@stdlib/utils/try-function' ); 127 128 function beep( str ) { 129 if ( typeof str !== 'string' ) { 130 throw new TypeError( 'invalid argument. Must provide a string primitive. Value: `' + str + '`.' ); 131 } 132 return 'beep ' + str; 133 } 134 135 function boop( str, clbk ) { 136 if ( typeof str !== 'string' ) { 137 throw new TypeError( 'invalid argument. Must provide a string primitive. Value: `' + str + '`.' ); 138 } 139 setTimeout( done, 1000 ); 140 141 function done() { 142 if ( str !== 'beep' ) { 143 throw new Error( 'invalid argument. String must equal `beep`. Value: `' + str + '`.' ); 144 } 145 clbk( str + ' boop' ); 146 } 147 } 148 149 function done( str ) { 150 if ( str !== 'beep boop' ) { 151 throw new Error( 'huh?' ); 152 } 153 } 154 155 var out; 156 var f; 157 158 // Synchronous... 159 f = wrap( beep ); 160 161 out = f( 'boop' ); 162 console.log( out ); 163 // => 'beep boop' 164 165 out = f( null ); 166 console.log( out.message ); 167 // => '...' 168 169 // Asynchronous... 170 f = wrap( boop ); 171 172 out = f( 'beep', done ); 173 console.log( out ); 174 // => undefined 175 176 out = f( 'foo', done ); 177 console.log( out ); 178 // => undefined 179 ``` 180 181 </section> 182 183 <!-- /.examples --> 184 185 <section class="links"> 186 187 </section> 188 189 <!-- /.links -->