vivth 1.4.1 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -12
- package/package.json +1 -1
- package/src/function/TryAsyncCall.mjs +11 -12
- package/src/function/TryCall.mjs +12 -14
- package/src/function/TryNew.mjs +11 -13
- package/types/src/function/TryAsyncCall.d.mts +5 -4
- package/types/src/function/TryCall.d.mts +6 -6
- package/types/src/function/TryNew.d.mts +5 -5
package/README.md
CHANGED
|
@@ -3410,7 +3410,8 @@ let [res, error] = await TryAsync(async () => {
|
|
|
3410
3410
|
/**
|
|
3411
3411
|
* @template {(...param:any[])=>Promise<any>} UNSAFEASYNCCALLBACK
|
|
3412
3412
|
* @param {UNSAFEASYNCCALLBACK} unsafeAsyncCallback
|
|
3413
|
-
* @
|
|
3413
|
+
* @param {Parameters<UNSAFEASYNCCALLBACK>} params
|
|
3414
|
+
* @returns {Promise<
|
|
3414
3415
|
* [Awaited<ReturnType<UNSAFEASYNCCALLBACK>>,undefined]|
|
|
3415
3416
|
* [undefined,Error]>}
|
|
3416
3417
|
*/
|
|
@@ -3422,11 +3423,12 @@ let [res, error] = await TryAsync(async () => {
|
|
|
3422
3423
|
import { TryAsyncCall } from "vivth";
|
|
3423
3424
|
|
|
3424
3425
|
(async () => {
|
|
3425
|
-
const [result, error] = await TryAsyncCall(
|
|
3426
|
+
const [result, error] = await TryAsyncCall(
|
|
3427
|
+
unsafeAsyncCallback,
|
|
3426
3428
|
...unsafeAsyncCallbackParameters,
|
|
3427
3429
|
);
|
|
3428
3430
|
if (!error) {
|
|
3429
|
-
// do something with result
|
|
3431
|
+
// do something with result safely;
|
|
3430
3432
|
}
|
|
3431
3433
|
})();
|
|
3432
3434
|
```
|
|
@@ -3444,9 +3446,9 @@ import { TryAsyncCall } from "vivth";
|
|
|
3444
3446
|
/**
|
|
3445
3447
|
* @template {(...param:any[])=>any} UNSAFECALLBACK
|
|
3446
3448
|
* @param {UNSAFECALLBACK} unsafeCallback
|
|
3447
|
-
* @
|
|
3448
|
-
* [ReturnType<UNSAFECALLBACK>,undefined]|
|
|
3449
|
-
* [undefined,Error]}
|
|
3449
|
+
* @param {Parameters<UNSAFECALLBACK>} params
|
|
3450
|
+
* @returns {[ReturnType<UNSAFECALLBACK>,undefined]|
|
|
3451
|
+
* [undefined, Error]}
|
|
3450
3452
|
*/
|
|
3451
3453
|
```
|
|
3452
3454
|
|
|
@@ -3455,9 +3457,9 @@ import { TryAsyncCall } from "vivth";
|
|
|
3455
3457
|
```js
|
|
3456
3458
|
import { TryCall } from "vivth";
|
|
3457
3459
|
|
|
3458
|
-
const [result, error] = TryCall(unsafeCallback
|
|
3460
|
+
const [result, error] = TryCall(unsafeCallback, ...unsafeCallbackParameters);
|
|
3459
3461
|
if (!error) {
|
|
3460
|
-
// do something with result
|
|
3462
|
+
// do something with result safely;
|
|
3461
3463
|
}
|
|
3462
3464
|
```
|
|
3463
3465
|
|
|
@@ -3474,8 +3476,8 @@ if (!error) {
|
|
|
3474
3476
|
/**
|
|
3475
3477
|
* @template {new (...args: any[]) => any} CLASSREF
|
|
3476
3478
|
* @param {CLASSREF} classReference
|
|
3477
|
-
* @
|
|
3478
|
-
* [InstanceType<CLASSREF>, undefined]|
|
|
3479
|
+
* @param {ConstructorParameters<CLASSREF>} params
|
|
3480
|
+
* @returns {[InstanceType<CLASSREF>, undefined]|
|
|
3479
3481
|
* [undefined, Error]}
|
|
3480
3482
|
*/
|
|
3481
3483
|
```
|
|
@@ -3485,9 +3487,9 @@ if (!error) {
|
|
|
3485
3487
|
```js
|
|
3486
3488
|
import { TryNew } from "vivth";
|
|
3487
3489
|
|
|
3488
|
-
const [instance, error] = TryNew(ClassReference
|
|
3490
|
+
const [instance, error] = TryNew(ClassReference, ...classConstructorParameters);
|
|
3489
3491
|
if (!error) {
|
|
3490
|
-
// do something with instance
|
|
3492
|
+
// do something with instance safely;
|
|
3491
3493
|
}
|
|
3492
3494
|
```
|
|
3493
3495
|
|
package/package.json
CHANGED
|
@@ -6,26 +6,25 @@
|
|
|
6
6
|
* - usefull to flatten your source code;
|
|
7
7
|
* @template {(...param:any[])=>Promise<any>} UNSAFEASYNCCALLBACK
|
|
8
8
|
* @param {UNSAFEASYNCCALLBACK} unsafeAsyncCallback
|
|
9
|
-
* @
|
|
9
|
+
* @param {Parameters<UNSAFEASYNCCALLBACK>} params
|
|
10
|
+
* @returns {Promise<
|
|
10
11
|
* [Awaited<ReturnType<UNSAFEASYNCCALLBACK>>,undefined]|
|
|
11
12
|
* [undefined,Error]>}
|
|
12
13
|
* @example
|
|
13
14
|
* import { TryAsyncCall } from 'vivth';
|
|
14
15
|
*
|
|
15
16
|
* (async() => {
|
|
16
|
-
* const [result, error] = await TryAsyncCall(unsafeAsyncCallback
|
|
17
|
+
* const [result, error] = await TryAsyncCall(unsafeAsyncCallback, ...unsafeAsyncCallbackParameters);
|
|
17
18
|
* if (!error) {
|
|
18
|
-
* // do something with result
|
|
19
|
+
* // do something with result safely;
|
|
19
20
|
* }
|
|
20
21
|
* })()
|
|
21
22
|
*/
|
|
22
|
-
export function TryAsyncCall(unsafeAsyncCallback) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
};
|
|
23
|
+
export async function TryAsyncCall(unsafeAsyncCallback, ...params) {
|
|
24
|
+
try {
|
|
25
|
+
return [await unsafeAsyncCallback(...params), undefined];
|
|
26
|
+
} catch (err) {
|
|
27
|
+
// @ts-expect-error
|
|
28
|
+
return [undefined, err];
|
|
29
|
+
}
|
|
31
30
|
}
|
package/src/function/TryCall.mjs
CHANGED
|
@@ -6,24 +6,22 @@
|
|
|
6
6
|
* - usefull to flatten your source code;
|
|
7
7
|
* @template {(...param:any[])=>any} UNSAFECALLBACK
|
|
8
8
|
* @param {UNSAFECALLBACK} unsafeCallback
|
|
9
|
-
* @
|
|
10
|
-
* [ReturnType<UNSAFECALLBACK>,undefined]|
|
|
11
|
-
* [undefined,Error]}
|
|
9
|
+
* @param {Parameters<UNSAFECALLBACK>} params
|
|
10
|
+
* @returns {[ReturnType<UNSAFECALLBACK>,undefined]|
|
|
11
|
+
* [undefined, Error]}
|
|
12
12
|
* @example
|
|
13
13
|
* import { TryCall } from 'vivth';
|
|
14
14
|
*
|
|
15
|
-
* const [result, error] = TryCall(unsafeCallback
|
|
15
|
+
* const [result, error] = TryCall(unsafeCallback, ...unsafeCallbackParameters);
|
|
16
16
|
* if (!error) {
|
|
17
|
-
*
|
|
17
|
+
* // do something with result safely;
|
|
18
18
|
* }
|
|
19
19
|
*/
|
|
20
|
-
export function TryCall(unsafeCallback) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
};
|
|
20
|
+
export function TryCall(unsafeCallback, ...params) {
|
|
21
|
+
try {
|
|
22
|
+
return [unsafeCallback(...params), undefined];
|
|
23
|
+
} catch (err) {
|
|
24
|
+
// @ts-expect-error
|
|
25
|
+
return [undefined, err];
|
|
26
|
+
}
|
|
29
27
|
}
|
package/src/function/TryNew.mjs
CHANGED
|
@@ -6,24 +6,22 @@
|
|
|
6
6
|
* - usefull to flatten your source code;
|
|
7
7
|
* @template {new (...args: any[]) => any} CLASSREF
|
|
8
8
|
* @param {CLASSREF} classReference
|
|
9
|
-
* @
|
|
10
|
-
* [InstanceType<CLASSREF>, undefined]|
|
|
9
|
+
* @param {ConstructorParameters<CLASSREF>} params
|
|
10
|
+
* @returns {[InstanceType<CLASSREF>, undefined]|
|
|
11
11
|
* [undefined, Error]}
|
|
12
12
|
* @example
|
|
13
13
|
* import { TryNew } from 'vivth';
|
|
14
14
|
*
|
|
15
|
-
* const [instance, error] = TryNew(ClassReference
|
|
15
|
+
* const [instance, error] = TryNew(ClassReference, ...classConstructorParameters)
|
|
16
16
|
* if(!error) {
|
|
17
|
-
*
|
|
17
|
+
* // do something with instance safely;
|
|
18
18
|
* }
|
|
19
19
|
*/
|
|
20
|
-
export function TryNew(classReference) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
};
|
|
20
|
+
export function TryNew(classReference, ...params) {
|
|
21
|
+
try {
|
|
22
|
+
return [new classReference(...params), undefined];
|
|
23
|
+
} catch (err) {
|
|
24
|
+
// @ts-expect-error
|
|
25
|
+
return [undefined, err];
|
|
26
|
+
}
|
|
29
27
|
}
|
|
@@ -4,17 +4,18 @@
|
|
|
4
4
|
* - usefull to flatten your source code;
|
|
5
5
|
* @template {(...param:any[])=>Promise<any>} UNSAFEASYNCCALLBACK
|
|
6
6
|
* @param {UNSAFEASYNCCALLBACK} unsafeAsyncCallback
|
|
7
|
-
* @
|
|
7
|
+
* @param {Parameters<UNSAFEASYNCCALLBACK>} params
|
|
8
|
+
* @returns {Promise<
|
|
8
9
|
* [Awaited<ReturnType<UNSAFEASYNCCALLBACK>>,undefined]|
|
|
9
10
|
* [undefined,Error]>}
|
|
10
11
|
* @example
|
|
11
12
|
* import { TryAsyncCall } from 'vivth';
|
|
12
13
|
*
|
|
13
14
|
* (async() => {
|
|
14
|
-
* const [result, error] = await TryAsyncCall(unsafeAsyncCallback
|
|
15
|
+
* const [result, error] = await TryAsyncCall(unsafeAsyncCallback, ...unsafeAsyncCallbackParameters);
|
|
15
16
|
* if (!error) {
|
|
16
|
-
* // do something with result
|
|
17
|
+
* // do something with result safely;
|
|
17
18
|
* }
|
|
18
19
|
* })()
|
|
19
20
|
*/
|
|
20
|
-
export function TryAsyncCall<UNSAFEASYNCCALLBACK extends (...param: any[]) => Promise<any>>(unsafeAsyncCallback: UNSAFEASYNCCALLBACK
|
|
21
|
+
export function TryAsyncCall<UNSAFEASYNCCALLBACK extends (...param: any[]) => Promise<any>>(unsafeAsyncCallback: UNSAFEASYNCCALLBACK, ...params: Parameters<UNSAFEASYNCCALLBACK>): Promise<[Awaited<ReturnType<UNSAFEASYNCCALLBACK>>, undefined] | [undefined, Error]>;
|
|
@@ -4,15 +4,15 @@
|
|
|
4
4
|
* - usefull to flatten your source code;
|
|
5
5
|
* @template {(...param:any[])=>any} UNSAFECALLBACK
|
|
6
6
|
* @param {UNSAFECALLBACK} unsafeCallback
|
|
7
|
-
* @
|
|
8
|
-
* [ReturnType<UNSAFECALLBACK>,undefined]|
|
|
9
|
-
* [undefined,Error]}
|
|
7
|
+
* @param {Parameters<UNSAFECALLBACK>} params
|
|
8
|
+
* @returns {[ReturnType<UNSAFECALLBACK>,undefined]|
|
|
9
|
+
* [undefined, Error]}
|
|
10
10
|
* @example
|
|
11
11
|
* import { TryCall } from 'vivth';
|
|
12
12
|
*
|
|
13
|
-
* const [result, error] = TryCall(unsafeCallback
|
|
13
|
+
* const [result, error] = TryCall(unsafeCallback, ...unsafeCallbackParameters);
|
|
14
14
|
* if (!error) {
|
|
15
|
-
*
|
|
15
|
+
* // do something with result safely;
|
|
16
16
|
* }
|
|
17
17
|
*/
|
|
18
|
-
export function TryCall<UNSAFECALLBACK extends (...param: any[]) => any>(unsafeCallback: UNSAFECALLBACK
|
|
18
|
+
export function TryCall<UNSAFECALLBACK extends (...param: any[]) => any>(unsafeCallback: UNSAFECALLBACK, ...params: Parameters<UNSAFECALLBACK>): [ReturnType<UNSAFECALLBACK>, undefined] | [undefined, Error];
|
|
@@ -4,15 +4,15 @@
|
|
|
4
4
|
* - usefull to flatten your source code;
|
|
5
5
|
* @template {new (...args: any[]) => any} CLASSREF
|
|
6
6
|
* @param {CLASSREF} classReference
|
|
7
|
-
* @
|
|
8
|
-
* [InstanceType<CLASSREF>, undefined]|
|
|
7
|
+
* @param {ConstructorParameters<CLASSREF>} params
|
|
8
|
+
* @returns {[InstanceType<CLASSREF>, undefined]|
|
|
9
9
|
* [undefined, Error]}
|
|
10
10
|
* @example
|
|
11
11
|
* import { TryNew } from 'vivth';
|
|
12
12
|
*
|
|
13
|
-
* const [instance, error] = TryNew(ClassReference
|
|
13
|
+
* const [instance, error] = TryNew(ClassReference, ...classConstructorParameters)
|
|
14
14
|
* if(!error) {
|
|
15
|
-
*
|
|
15
|
+
* // do something with instance safely;
|
|
16
16
|
* }
|
|
17
17
|
*/
|
|
18
|
-
export function TryNew<CLASSREF extends new (...args: any[]) => any>(classReference: CLASSREF
|
|
18
|
+
export function TryNew<CLASSREF extends new (...args: any[]) => any>(classReference: CLASSREF, ...params: ConstructorParameters<CLASSREF>): [InstanceType<CLASSREF>, undefined] | [undefined, Error];
|