Skip to content

Commit 32de612

Browse files
committed
Add Typescript definition
1 parent 6dec8a7 commit 32de612

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Callback function.
23+
*
24+
* @param error - encountered error or null
25+
* @param args - results
26+
*/
27+
type Callback = ( error: Error | null, ...args: Array<any> ) => void;
28+
29+
/**
30+
* Function to invoke if `x` returns an error.
31+
*
32+
* @param clbk - callback to invoke upon function completion
33+
*/
34+
type Unary = ( clbk: Callback ) => void;
35+
36+
/**
37+
* Function to invoke if `x` returns an error.
38+
*
39+
* @param error - the error from `x`
40+
* @param clbk - callback to invoke upon function completion
41+
*/
42+
type Binary = ( error: Error, clbk: Callback ) => void;
43+
44+
/**
45+
* Function to invoke if `x` returns an error.
46+
*
47+
* @param error - the error from `x`
48+
* @param clbk - callback to invoke upon function completion
49+
*/
50+
type ThenFunction = Unary | Binary;
51+
52+
/**
53+
* First function to invoke.
54+
*
55+
* @param clbk - callback to invoke upon function completion
56+
*/
57+
type TryFunction = ( clbk: Callback ) => void;
58+
59+
/**
60+
* If a function does not return an error, invokes a callback with the function result; otherwise, invokes a second function `y`.
61+
*
62+
* @param x - function to invoke
63+
* @param y - function to invoke if `x` returns an error
64+
* @param done - callback to invoke upon completion
65+
*
66+
* @example
67+
* var randu = require( `@stdlib/random/base/randu` );
68+
*
69+
* function x( clbk ) {
70+
* setTimeout( onTimeout, 0 );
71+
* function onTimeout() {
72+
* if ( randu() > 0.5 ) {
73+
* return clbk( null, 1.0 );
74+
* }
75+
* clbk( new Error( 'beep' ) );
76+
* }
77+
* }
78+
*
79+
* function y( clbk ) {
80+
* setTimeout( onTimeout, 0 );
81+
* function onTimeout() {
82+
* clbk( null, -1.0 );
83+
* }
84+
* }
85+
*
86+
* function done( error, result ) {
87+
* if ( error ) {
88+
* throw error;
89+
* }
90+
* console.log( result );
91+
* }
92+
*
93+
* trythenAsync( x, y, done );
94+
*/
95+
declare function trythenAsync( x: TryFunction, y: ThenFunction, done: Callback ): void; // tslint-disable-line max-line-length
96+
97+
98+
// EXPORTS //
99+
100+
export = trythenAsync;
101+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}

lib/node_modules/@stdlib/utils/async/try-then/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {

0 commit comments

Comments
 (0)