|
| 1 | +/* |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2021 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 | +import randu = require( '@stdlib/random/base/randu' ); |
| 20 | +import trythenAsync = require( './index' ); |
| 21 | + |
| 22 | +const x = ( clbk: Function ) => { |
| 23 | + setTimeout( onTimeout, 0 ); |
| 24 | + function onTimeout() { |
| 25 | + if ( randu() > 0.5 ) { |
| 26 | + return clbk( null, 1.0 ); |
| 27 | + } |
| 28 | + clbk( new Error( 'oops' ) ); |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +const y = ( clbk: Function ) => { |
| 33 | + setTimeout( onTimeout, 0 ); |
| 34 | + function onTimeout() { |
| 35 | + clbk( null, -1.0 ); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +const done = ( error: Error | null, result: number ) => { |
| 40 | + if ( error ) { |
| 41 | + throw error; |
| 42 | + } |
| 43 | + console.log( result ); |
| 44 | +}; |
| 45 | + |
| 46 | +// TESTS // |
| 47 | + |
| 48 | +// The function does not return a value... |
| 49 | +{ |
| 50 | + trythenAsync( x, y, done ); // $ExpectType void |
| 51 | +} |
| 52 | + |
| 53 | +// The compiler throws an error if the function is provided a first argument which is not a function having a supported signature... |
| 54 | +{ |
| 55 | + trythenAsync( 2, y, done ); // $ExpectError |
| 56 | + trythenAsync( false, y, done ); // $ExpectError |
| 57 | + trythenAsync( true, y, done ); // $ExpectError |
| 58 | + trythenAsync( 'abc', y, done ); // $ExpectError |
| 59 | + trythenAsync( {}, y, done ); // $ExpectError |
| 60 | + trythenAsync( [], y, done ); // $ExpectError |
| 61 | + trythenAsync( ( x: number ): number => x, y, done ); // $ExpectError |
| 62 | +} |
| 63 | + |
| 64 | +// The compiler throws an error if the function is provided a second argument which is not a function having a supported signature... |
| 65 | +{ |
| 66 | + trythenAsync( x, 2, done ); // $ExpectError |
| 67 | + trythenAsync( x, false, done ); // $ExpectError |
| 68 | + trythenAsync( x, true, done ); // $ExpectError |
| 69 | + trythenAsync( x, 'abc', done ); // $ExpectError |
| 70 | + trythenAsync( x, {}, done ); // $ExpectError |
| 71 | + trythenAsync( x, [], done ); // $ExpectError |
| 72 | + trythenAsync( x, ( x: number ): number => x, done ); // $ExpectError |
| 73 | +} |
| 74 | + |
| 75 | +// The compiler throws an error if the function is provided a third argument which is not a callback function... |
| 76 | +{ |
| 77 | + trythenAsync( x, y, 2 ); // $ExpectError |
| 78 | + trythenAsync( x, y, false ); // $ExpectError |
| 79 | + trythenAsync( x, y, true ); // $ExpectError |
| 80 | + trythenAsync( x, y, 'abc' ); // $ExpectError |
| 81 | + trythenAsync( x, y, {} ); // $ExpectError |
| 82 | + trythenAsync( x, y, [] ); // $ExpectError |
| 83 | + trythenAsync( x, y, ( x: number ): number => x ); // $ExpectError |
| 84 | +} |
| 85 | + |
| 86 | +// The compiler throws an error if the function is provided an invalid number of arguments... |
| 87 | +{ |
| 88 | + trythenAsync(); // $ExpectError |
| 89 | + trythenAsync( x ); // $ExpectError |
| 90 | + trythenAsync( x, y ); // $ExpectError |
| 91 | + trythenAsync( x, y, done, {} ); // $ExpectError |
| 92 | +} |
0 commit comments