time-to-botec

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

until_async.js (3165B)


      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 * Invokes a function until a test condition is true.
     30 *
     31 * @param {Function} predicate - function which indicates whether to continue invoking a function
     32 * @param {Function} fcn - function to invoke
     33 * @param {Callback} done - callback to invoke upon completion
     34 * @param {*} [thisArg] - execution context for the invoked function
     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 *
     39 * @example
     40 * function predicate( i, clbk ) {
     41 *     clbk( null, i >= 5 );
     42 * }
     43 *
     44 * function fcn( i, next ) {
     45 *     setTimeout( onTimeout, i );
     46 *     function onTimeout() {
     47 *         console.log( 'beep: %d', i );
     48 *         next();
     49 *     }
     50 * }
     51 *
     52 * function done( error ) {
     53 *     if ( error ) {
     54 *         throw error;
     55 *     }
     56 * }
     57 *
     58 * untilAsync( predicate, fcn, done );
     59 */
     60 function untilAsync( predicate, fcn, done, thisArg ) {
     61 	var args;
     62 	var idx;
     63 	if ( !isFunction( predicate ) ) {
     64 		throw new TypeError( 'invalid argument. First argument must be a function. Value: `'+predicate+'`.' );
     65 	}
     66 	if ( !isFunction( fcn ) ) {
     67 		throw new TypeError( 'invalid argument. Second argument must be a function. Value: `'+fcn+'`.' );
     68 	}
     69 	if ( !isFunction( done ) ) {
     70 		throw new TypeError( 'invalid argument. Third argument must be a function. Value: `'+done+'`.' );
     71 	}
     72 	args = [];
     73 	idx = 0;
     74 	predicate( idx, onPredicate );
     75 
     76 	/**
     77 	* Callback invoked upon a predicate result.
     78 	*
     79 	* @private
     80 	* @param {(Error|null)} error - error object
     81 	* @param {boolean} result - predicate result
     82 	* @returns {void}
     83 	*/
     84 	function onPredicate( error, result ) {
     85 		if ( error ) {
     86 			return done( error );
     87 		}
     88 		if ( !result ) {
     89 			return fcn.call( thisArg, idx, next );
     90 		}
     91 		if ( args.length ) {
     92 			args.unshift( null ); // error argument
     93 		}
     94 		done.apply( null, args );
     95 	}
     96 
     97 	/**
     98 	* Callback invoked upon completion of a provided function.
     99 	*
    100 	* @private
    101 	* @param {(Error|null)} error - error object
    102 	* @param {...*} results - function results
    103 	* @returns {void}
    104 	*/
    105 	function next( error ) {
    106 		var i;
    107 		if ( error ) {
    108 			return done( error );
    109 		}
    110 		idx += 1;
    111 
    112 		// Cache the most recent results...
    113 		if ( arguments.length > 1 ) {
    114 			args = new Array( arguments.length-1 );
    115 			for ( i = 1; i < arguments.length; i++ ) {
    116 				args[ i-1 ] = arguments[ i ];
    117 			}
    118 		}
    119 		// Run the test condition:
    120 		predicate( idx, onPredicate );
    121 	}
    122 }
    123 
    124 
    125 // EXPORTS //
    126 
    127 module.exports = untilAsync;