README.md (6039B)
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 # trythenAsync 22 23 > If a function does not return an error, invoke a callback with the function result; otherwise, invoke 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 trythenAsync = require( '@stdlib/utils/async/try-then' ); 41 ``` 42 43 #### trythenAsync( 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 second function `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 y( clbk ) { 61 setTimeout( onTimeout, 0 ); 62 function onTimeout() { 63 clbk( null, -1.0 ); 64 } 65 } 66 67 function done( error, result ) { 68 if ( error ) { 69 throw error; 70 } 71 console.log( result ); 72 } 73 74 trythenAsync( x, y, done ); 75 ``` 76 77 The function `x` is provided a single argument: 78 79 - `clbk`: callback to invoke upon function completion 80 81 The callback function accepts any number of arguments, with the first argument reserved for providing an error. If the error argument is falsy, the `done` callback is invoked with its first argument as `null` and all other provided arguments. 82 83 ```javascript 84 var randu = require( '@stdlib/random/base/randu' ); 85 86 function x( clbk ) { 87 setTimeout( onTimeout, 0 ); 88 function onTimeout() { 89 clbk( null, 1.0, 2.0, 3.0 ); 90 } 91 } 92 93 function y( clbk ) { 94 setTimeout( onTimeout, 0 ); 95 function onTimeout() { 96 clbk( null, 4.0, 5.0, 6.0 ); 97 } 98 } 99 100 function done( error, a, b, c ) { 101 if ( error ) { 102 throw error; 103 } 104 console.log( a, b, c ); 105 } 106 107 trythenAsync( x, y, done ); 108 ``` 109 110 If the error argument is truthy, the function invokes `y`. The number of arguments provided to `y` depends on the function's `length`. If `y` is a unary function, `y` is provided a single argument: 111 112 - `clbk`: callback to invoke upon function completion 113 114 Otherwise, `y` is provided two arguments: 115 116 - `error`: the error from `x` 117 - `clbk`: callback to invoke upon function completion 118 119 The callback function accepts any number of arguments, with the first argument reserved for providing an error. If the error argument is falsy, the `done` callback is invoked with its first argument equal to `null` and all other provided arguments. If the error argument is truthy, the `done` callback is invoked with only the error argument provided by `y`. 120 121 ```javascript 122 var randu = require( '@stdlib/random/base/randu' ); 123 124 function x( clbk ) { 125 setTimeout( onTimeout, 0 ); 126 function onTimeout() { 127 if ( randu() > 0.5 ) { 128 return clbk( null, 1.0, 2.0, 3.0 ); 129 } 130 clbk( new Error( 'beep' ) ); 131 } 132 } 133 134 function y( clbk ) { 135 setTimeout( onTimeout, 0 ); 136 function onTimeout() { 137 if ( randu() > 0.5 ) { 138 return clbk( null, 4.0, 5.0, 6.0 ); 139 } 140 clbk( new Error( 'boop' ) ); 141 } 142 } 143 144 function done( error, a, b, c ) { 145 if ( error ) { 146 console.error( error.message ); 147 } 148 console.log( a, b, c ); 149 } 150 151 trythenAsync( x, y, done ); 152 ``` 153 154 </section> 155 156 <!-- /.usage --> 157 158 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 159 160 <section class="notes"> 161 162 ## Notes 163 164 - 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`). 165 166 </section> 167 168 <!-- /.notes --> 169 170 <!-- Package usage examples. --> 171 172 <section class="examples"> 173 174 ## Examples 175 176 <!-- eslint no-undef: "error" --> 177 178 ```javascript 179 var randu = require( '@stdlib/random/base/randu' ); 180 var ceil = require( '@stdlib/math/base/special/ceil' ); 181 var repeatString = require( '@stdlib/string/repeat' ); 182 var trythenAsync = require( '@stdlib/utils/async/try-then' ); 183 184 var i; 185 186 function next() { 187 trythenAsync( x, y, done ); 188 } 189 190 function x( clbk ) { 191 setTimeout( onTimeout, 0 ); 192 function onTimeout() { 193 if ( randu() > 0.9 ) { 194 return clbk( null, repeatString( 'BOOP', ceil( randu()*3.0 ) ) ); 195 } 196 clbk( new Error( 'oops' ) ); 197 } 198 } 199 200 function y( clbk ) { 201 setTimeout( onTimeout, 0 ); 202 function onTimeout() { 203 clbk( null, repeatString( 'beep', ceil( randu()*5.0 ) ) ); 204 } 205 } 206 207 function done( error, result ) { 208 if ( error ) { 209 throw error; 210 } 211 i += 1; 212 console.log( result ); 213 if ( i < 100 ) { 214 return next(); 215 } 216 } 217 218 i = 0; 219 next(); 220 ``` 221 222 </section> 223 224 <!-- /.examples --> 225 226 <!-- 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. --> 227 228 <section class="references"> 229 230 </section> 231 232 <!-- /.references --> 233 234 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 235 236 <section class="links"> 237 238 </section> 239 240 <!-- /.links -->