time-to-botec

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

README.md (4633B)


      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 # Waterfall
     22 
     23 > Execute functions in series, passing the results of one function as arguments to the next 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 waterfall = require( '@stdlib/utils/async/series-waterfall' );
     41 ```
     42 
     43 #### waterfall( fcns, clbk\[, thisArg] )
     44 
     45 Executes `functions` in series, passing the results of one `function` as arguments to the next `function`.
     46 
     47 ```javascript
     48 function foo( next ) {
     49     next( null, 'beep' );
     50 }
     51 
     52 function bar( str, next ) {
     53     console.log( str );
     54     // => 'beep'
     55 
     56     next();
     57 }
     58 
     59 function done( error ) {
     60     if ( error ) {
     61         throw error;
     62     }
     63 }
     64 
     65 var fcns = [ foo, bar ];
     66 
     67 waterfall( fcns, done );
     68 ```
     69 
     70 To set the `this` context for **all** `functions` in the provided function array, provide a `thisArg`.
     71 
     72 <!-- eslint-disable no-use-before-define -->
     73 
     74 ```javascript
     75 function foo( next ) {
     76     this.idx = 0;
     77     next( null, 'beep' );
     78 }
     79 
     80 function bar( str, next ) {
     81     this.idx += 1;
     82     console.log( str );
     83     // => 'beep'
     84 
     85     next();
     86 }
     87 
     88 function done( error ) {
     89     if ( error ) {
     90         throw error;
     91     }
     92     console.log( ctx.idx );
     93     // => 1
     94 }
     95 
     96 var ctx = {};
     97 var fcns = [ foo, bar ];
     98 
     99 waterfall( fcns, done, ctx );
    100 ```
    101 
    102 #### waterfall.factory( fcns, done\[, thisArg] )
    103 
    104 Returns a reusable waterfall `function`.
    105 
    106 ```javascript
    107 function foo( next ) {
    108     next( null, 'beep' );
    109 }
    110 
    111 function bar( str, next ) {
    112     console.log( str );
    113     // => 'beep'
    114 
    115     next();
    116 }
    117 
    118 function done( error ) {
    119     if ( error ) {
    120         throw error;
    121     }
    122 }
    123 
    124 var fcns = [ foo, bar ];
    125 
    126 var run = waterfall.factory( fcns, done );
    127 
    128 run();
    129 run();
    130 run();
    131 ```
    132 
    133 </section>
    134 
    135 <!-- /.usage -->
    136 
    137 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    138 
    139 <section class="notes">
    140 
    141 ## Notes
    142 
    143 -   The last argument applied to each waterfall `function` is a callback. The callback should be invoked upon a series `function` completion. The first argument is reserved as an `error` argument (which can be `null`). Any results which should be passed to the next `function` in the series should be provided beginning with the second argument.
    144 -   If any `function` calls the provided callback with a truthy `error` argument, the waterfall suspends execution and immediately calls the `done` callback for subsequent `error` handling.
    145 -   This implementation does **not** guarantee that execution is asynchronous. To do so, 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`).
    146 
    147 </section>
    148 
    149 <!-- /.notes -->
    150 
    151 <!-- Package usage examples. -->
    152 
    153 <section class="examples">
    154 
    155 ## Examples
    156 
    157 <!-- eslint no-undef: "error" -->
    158 
    159 ```javascript
    160 var replace = require( '@stdlib/string/replace' );
    161 var waterfall = require( '@stdlib/utils/async/series-waterfall' );
    162 
    163 function foo( next ) {
    164     next( null, 'beep' );
    165 }
    166 
    167 function bar( str, next ) {
    168     console.log( str );
    169     next( null, replace( str, 'e', 'o' ) );
    170 }
    171 
    172 function fun( str, next ) {
    173     console.log( str );
    174     next();
    175 }
    176 
    177 function done( error ) {
    178     if ( error ) {
    179         throw error;
    180     }
    181     console.log( 'done' );
    182 }
    183 
    184 var fcns = [ foo, bar, fun ];
    185 
    186 waterfall( fcns, done );
    187 ```
    188 
    189 </section>
    190 
    191 <!-- /.examples -->
    192 
    193 <!-- 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. -->
    194 
    195 <section class="references">
    196 
    197 </section>
    198 
    199 <!-- /.references -->
    200 
    201 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    202 
    203 <section class="links">
    204 
    205 </section>
    206 
    207 <!-- /.links -->