README.md (3658B)
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 # ifelseAsync 22 23 > If a predicate function returns a truthy value, return `x`; otherwise, return `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 ifelseAsync = require( '@stdlib/utils/async/if-else' ); 41 ``` 42 43 #### ifelseAsync( predicate, x, y, done ) 44 45 If a `predicate` function returns a truthy value, returns `x`; otherwise, returns `y`. 46 47 ```javascript 48 var randu = require( '@stdlib/random/base/randu' ); 49 50 function predicate( clbk ) { 51 setTimeout( onTimeout, 0 ); 52 function onTimeout() { 53 clbk( null, randu() > 0.5 ); 54 } 55 } 56 57 function done( error, result ) { 58 if ( error ) { 59 throw error; 60 } 61 console.log( result ); 62 } 63 64 ifelseAsync( predicate, 1.0, -1.0, done ); 65 ``` 66 67 The `predicate` function is provided a single argument: 68 69 - `clbk`: callback to invoke upon `predicate` function completion 70 71 The callback accepts two arguments: 72 73 - `error`: error object 74 - `bool`: condition used to determine whether to return `x` or `y` 75 76 The `done` callback is invoked upon function completion and is provided at most two arguments: 77 78 - `error`: error object 79 - `result`: either `x` or `y` 80 81 </section> 82 83 <!-- /.usage --> 84 85 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 86 87 <section class="notes"> 88 89 ## Notes 90 91 - 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`). 92 93 </section> 94 95 <!-- /.notes --> 96 97 <!-- Package usage examples. --> 98 99 <section class="examples"> 100 101 ## Examples 102 103 <!-- eslint-disable callback-return --> 104 105 <!-- eslint no-undef: "error" --> 106 107 ```javascript 108 var randu = require( '@stdlib/random/base/randu' ); 109 var ifelseAsync = require( '@stdlib/utils/async/if-else' ); 110 111 var i; 112 113 function next() { 114 ifelseAsync( predicate, 'BOOP', 'beep', done ); 115 } 116 117 function predicate( clbk ) { 118 setTimeout( onTimeout, 0 ); 119 function onTimeout() { 120 clbk( null, randu() > 0.9 ); 121 } 122 } 123 124 function done( error, result ) { 125 if ( error ) { 126 throw error; 127 } 128 i += 1; 129 console.log( result ); 130 if ( i < 100 ) { 131 return next(); 132 } 133 } 134 135 i = 0; 136 next(); 137 ``` 138 139 </section> 140 141 <!-- /.examples --> 142 143 <!-- 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. --> 144 145 <section class="references"> 146 147 </section> 148 149 <!-- /.references --> 150 151 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 152 153 <section class="links"> 154 155 </section> 156 157 <!-- /.links -->