time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (5415B)


      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 # ifthenAsync
     22 
     23 > If a predicate function returns a truthy value, invoke `x`; otherwise, invoke `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 ifthenAsync = require( '@stdlib/utils/async/if-then' );
     41 ```
     42 
     43 #### ifthenAsync( predicate, x, y, done )
     44 
     45 If a `predicate` function returns a truthy value, invokes `x`; otherwise, invokes `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 x( clbk ) {
     58     setTimeout( onTimeout, 0 );
     59     function onTimeout() {
     60         clbk( null, 1.0 );
     61     }
     62 }
     63 
     64 function y( clbk ) {
     65     setTimeout( onTimeout, 0 );
     66     function onTimeout() {
     67         clbk( null, -1.0 );
     68     }
     69 }
     70 
     71 function done( error, result ) {
     72     if ( error ) {
     73         throw error;
     74     }
     75     console.log( result );
     76 }
     77 
     78 ifthenAsync( predicate, x, y, done );
     79 ```
     80 
     81 The `predicate` function is provided a single argument:
     82 
     83 -   `clbk`: callback to invoke upon `predicate` function completion
     84 
     85 The callback accepts two arguments:
     86 
     87 -   `error`: error object
     88 -   `bool`: condition used to determine whether to invoke `x` or `y`
     89 
     90 Both `x` and `y` are provided a single argument:
     91 
     92 -   `clbk`: callback to invoke upon function completion
     93 
     94 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. If the error argument is truthy, the `done` callback is invoked with only an error argument.
     95 
     96 ```javascript
     97 var randu = require( '@stdlib/random/base/randu' );
     98 
     99 function predicate( clbk ) {
    100     setTimeout( onTimeout, 0 );
    101     function onTimeout() {
    102         clbk( null, randu() > 0.5 );
    103     }
    104 }
    105 
    106 function x( clbk ) {
    107     setTimeout( onTimeout, 0 );
    108     function onTimeout() {
    109         clbk( null, 1.0, 2.0, 3.0 );
    110     }
    111 }
    112 
    113 function y( clbk ) {
    114     setTimeout( onTimeout, 0 );
    115     function onTimeout() {
    116         clbk( null, 4.0, 5.0, 6.0 );
    117     }
    118 }
    119 
    120 function done( error, a, b, c ) {
    121     if ( error ) {
    122         throw error;
    123     }
    124     console.log( a, b, c );
    125 }
    126 
    127 ifthenAsync( predicate, x, y, done );
    128 ```
    129 
    130 </section>
    131 
    132 <!-- /.usage -->
    133 
    134 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    135 
    136 <section class="notes">
    137 
    138 ## Notes
    139 
    140 -   The function is similar to [`ifelseAsync()`][@stdlib/utils/async/if-else], but allows deferred argument evaluation.
    141 -   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`).
    142 
    143 </section>
    144 
    145 <!-- /.notes -->
    146 
    147 <!-- Package usage examples. -->
    148 
    149 <section class="examples">
    150 
    151 ## Examples
    152 
    153 <!-- eslint-disable callback-return -->
    154 
    155 <!-- eslint no-undef: "error" -->
    156 
    157 ```javascript
    158 var randu = require( '@stdlib/random/base/randu' );
    159 var ceil = require( '@stdlib/math/base/special/ceil' );
    160 var repeatString = require( '@stdlib/string/repeat' );
    161 var ifthenAsync = require( '@stdlib/utils/async/if-then' );
    162 
    163 var i;
    164 
    165 function next() {
    166     ifthenAsync( predicate, x, y, done );
    167 }
    168 
    169 function predicate( clbk ) {
    170     setTimeout( onTimeout, 0 );
    171     function onTimeout() {
    172         clbk( null, randu() > 0.9 );
    173     }
    174 }
    175 
    176 function x( clbk ) {
    177     setTimeout( onTimeout, 0 );
    178     function onTimeout() {
    179         clbk( null, repeatString( 'BOOP', ceil( randu()*3.0 ) ) );
    180     }
    181 }
    182 
    183 function y( clbk ) {
    184     setTimeout( onTimeout, 0 );
    185     function onTimeout() {
    186         clbk( null, repeatString( 'beep', ceil( randu()*5.0 ) ) );
    187     }
    188 }
    189 
    190 function done( error, result ) {
    191     if ( error ) {
    192         throw error;
    193     }
    194     i += 1;
    195     console.log( result );
    196     if ( i < 100 ) {
    197         return next();
    198     }
    199 }
    200 
    201 i = 0;
    202 next();
    203 ```
    204 
    205 </section>
    206 
    207 <!-- /.examples -->
    208 
    209 <!-- 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. -->
    210 
    211 <section class="references">
    212 
    213 </section>
    214 
    215 <!-- /.references -->
    216 
    217 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    218 
    219 <section class="links">
    220 
    221 [@stdlib/utils/async/if-else]: https://www.npmjs.com/package/@stdlib/utils/tree/main/async/if-else
    222 
    223 </section>
    224 
    225 <!-- /.links -->