time-to-botec

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

if_then_async.js (3329B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isFunction = require( '@stdlib/assert/is-function' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * If a predicate function returns a truthy value, invokes `x`; otherwise, invokes `y`.
     30 *
     31 * @param {Function} predicate - predicate function
     32 * @param {Function} x - function to invoke if a condition is truthy
     33 * @param {Function} y - function to invoke if a condition is falsy
     34 * @param {Function} done - callback to invoke upon completion
     35 * @throws {TypeError} first argument must be a function
     36 * @throws {TypeError} second argument must be a function
     37 * @throws {TypeError} third argument must be a function
     38 * @throws {TypeError} last argument must be a function
     39 *
     40 * @example
     41 * var randu = require( '@stdlib/random/base/randu' );
     42 *
     43 * function predicate( clbk ) {
     44 *     setTimeout( onTimeout, 0 );
     45 *     function onTimeout() {
     46 *         clbk( null, randu() > 0.5 );
     47 *     }
     48 * }
     49 *
     50 * function x( clbk ) {
     51 *     setTimeout( onTimeout, 0 );
     52 *     function onTimeout() {
     53 *         clbk( null, 1.0 );
     54 *     }
     55 * }
     56 *
     57 * function y( clbk ) {
     58 *     setTimeout( onTimeout, 0 );
     59 *     function onTimeout() {
     60 *         clbk( null, -1.0 );
     61 *     }
     62 * }
     63 *
     64 * function done( error, result ) {
     65 *     if ( error ) {
     66 *         throw error;
     67 *     }
     68 *     console.log( result );
     69 * }
     70 * ifthenAsync( predicate, x, y, done );
     71 */
     72 function ifthenAsync( predicate, x, y, done ) {
     73 	if ( !isFunction( predicate ) ) {
     74 		throw new TypeError( 'invalid argument. First argument must be a function. Value: `'+predicate+'`.' );
     75 	}
     76 	if ( !isFunction( x ) ) {
     77 		throw new TypeError( 'invalid argument. Second argument must be a function. Value: `'+x+'`.' );
     78 	}
     79 	if ( !isFunction( y ) ) {
     80 		throw new TypeError( 'invalid argument. Third argument must be a function. Value: `'+y+'`.' );
     81 	}
     82 	if ( !isFunction( done ) ) {
     83 		throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' );
     84 	}
     85 	predicate( clbk1 );
     86 
     87 	/**
     88 	* Callback invoked by a predicate function.
     89 	*
     90 	* @private
     91 	* @param {(Error|null)} error - error object
     92 	* @param {boolean} bool - condition
     93 	* @returns {void}
     94 	*/
     95 	function clbk1( error, bool ) {
     96 		if ( error ) {
     97 			return done( error );
     98 		}
     99 		if ( bool ) {
    100 			return x( clbk2 );
    101 		}
    102 		y( clbk2 );
    103 	}
    104 
    105 	/**
    106 	* Callback invoked by either `x` or `y`.
    107 	*
    108 	* @private
    109 	* @param {(Error|null)} error - error object
    110 	* @param {...*} args - results
    111 	* @returns {void}
    112 	*/
    113 	function clbk2( error ) {
    114 		var nargs;
    115 		var args;
    116 		var i;
    117 		if ( error ) {
    118 			return done( error );
    119 		}
    120 		nargs = arguments.length;
    121 		args = new Array( nargs );
    122 		args[ 0 ] = null;
    123 		for ( i = 1; i < nargs; i++ ) {
    124 			args[ i ] = arguments[ i ];
    125 		}
    126 		done.apply( null, args );
    127 	}
    128 }
    129 
    130 
    131 // EXPORTS //
    132 
    133 module.exports = ifthenAsync;