try_then_async.js (3220B)
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 function does not return an error, invokes a callback with the function result; otherwise, invokes a second function `y`. 30 * 31 * @param {Function} x - function to invoke 32 * @param {Function} y - function to invoke if `x` returns an error 33 * @param {Function} done - callback to invoke upon completion 34 * @throws {TypeError} first argument must be a function 35 * @throws {TypeError} second argument must be a function 36 * @throws {TypeError} last argument must be a function 37 * 38 * @example 39 * var randu = require( '@stdlib/random/base/randu' ); 40 * 41 * function x( clbk ) { 42 * setTimeout( onTimeout, 0 ); 43 * function onTimeout() { 44 * if ( randu() > 0.5 ) { 45 * return clbk( null, 1.0 ); 46 * } 47 * clbk( new Error( 'beep' ) ); 48 * } 49 * } 50 * 51 * function y( clbk ) { 52 * setTimeout( onTimeout, 0 ); 53 * function onTimeout() { 54 * clbk( null, -1.0 ); 55 * } 56 * } 57 * 58 * function done( error, result ) { 59 * if ( error ) { 60 * throw error; 61 * } 62 * console.log( result ); 63 * } 64 * 65 * trythenAsync( x, y, done ); 66 */ 67 function trythenAsync( x, y, done ) { 68 if ( !isFunction( x ) ) { 69 throw new TypeError( 'invalid argument. First argument must be a function. Value: `'+x+'`.' ); 70 } 71 if ( !isFunction( y ) ) { 72 throw new TypeError( 'invalid argument. Second argument must be a function. Value: `'+y+'`.' ); 73 } 74 if ( !isFunction( done ) ) { 75 throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' ); 76 } 77 x( clbk1 ); 78 79 /** 80 * Callback invoked by `x`. 81 * 82 * @private 83 * @param {(Error|null)} error - error object 84 * @param {...*} args - function results 85 * @returns {void} 86 */ 87 function clbk1( error ) { 88 var nargs; 89 var args; 90 var i; 91 if ( error ) { 92 if ( y.length === 1 ) { 93 return y( clbk2 ); 94 } 95 return y( error, clbk2 ); 96 } 97 nargs = arguments.length; 98 args = new Array( nargs ); 99 args[ 0 ] = null; 100 for ( i = 1; i < nargs; i++ ) { 101 args[ i ] = arguments[ i ]; 102 } 103 return done.apply( null, args ); 104 } 105 106 /** 107 * Callback invoked by `y`. 108 * 109 * @private 110 * @param {(Error|null)} error - error object 111 * @param {...*} args - function results 112 * @returns {void} 113 */ 114 function clbk2( error ) { 115 var nargs; 116 var args; 117 var i; 118 if ( error ) { 119 return done( error ); 120 } 121 nargs = arguments.length; 122 args = new Array( nargs ); 123 args[ 0 ] = null; 124 for ( i = 1; i < nargs; i++ ) { 125 args[ i ] = arguments[ i ]; 126 } 127 return done.apply( null, args ); 128 } 129 } 130 131 132 // EXPORTS // 133 134 module.exports = trythenAsync;