time-to-botec

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

async.js (1723B)


      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 fs = require( 'fs' );
     24 
     25 
     26 // FUNCTIONS //
     27 
     28 var fcn;
     29 if ( typeof fs.access === 'function' ) {
     30 	fcn = fs.access;
     31 } else {
     32 	fcn = fs.stat;
     33 }
     34 
     35 
     36 // MAIN //
     37 
     38 /**
     39 * Tests whether a path exists on the filesystem.
     40 *
     41 * @param {(string|Buffer)} path - path to test
     42 * @param {Function} clbk - callback to invoke after testing path existence
     43 *
     44 * @example
     45 * exists( __dirname, done );
     46 *
     47 * function done( error, bool ) {
     48 *     if ( error ) {
     49 *         console.error( error );
     50 *     }
     51 *     if ( bool ) {
     52 *         console.log( '...path exists.' );
     53 *     } else {
     54 *         console.log( '...path does not exist.' );
     55 *     }
     56 * }
     57 */
     58 function exists( path, clbk ) {
     59 	fcn( path, done );
     60 
     61 	/**
     62 	* Callback invoked upon performing a filesystem call.
     63 	*
     64 	* @private
     65 	* @param {(Error|null)} error - error object
     66 	* @returns {void}
     67 	*/
     68 	function done( error ) {
     69 		if ( clbk.length === 2 ) {
     70 			if ( error ) {
     71 				return clbk( error, false );
     72 			}
     73 			return clbk( null, true );
     74 		}
     75 		if ( error ) {
     76 			return clbk( false );
     77 		}
     78 		return clbk( true );
     79 	}
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = exists;