@ceale/util 1.12.0 → 1.12.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/dist/cjs/index.js +16 -0
- package/dist/esm/index.js +16 -0
- package/dist/types/error.d.ts +38 -0
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -32,6 +32,7 @@ __export(exports_src, {
|
|
|
32
32
|
waitSync: () => waitSync,
|
|
33
33
|
wait: () => wait,
|
|
34
34
|
uri: () => uri,
|
|
35
|
+
tryCatch: () => tryCatch,
|
|
35
36
|
throttle: () => throttle,
|
|
36
37
|
sleepAsync: () => sleepAsync,
|
|
37
38
|
sleep: () => sleep,
|
|
@@ -409,3 +410,18 @@ class CubicBezier {
|
|
|
409
410
|
}
|
|
410
411
|
}
|
|
411
412
|
}
|
|
413
|
+
// src/error.ts
|
|
414
|
+
var tryCatch = (parameter) => {
|
|
415
|
+
if (parameter instanceof Promise) {
|
|
416
|
+
return parameter.then((data) => Object.assign([data, null], { data, error: null })).catch((error) => Object.assign([null, error], { data: null, error }));
|
|
417
|
+
}
|
|
418
|
+
if (typeof parameter === "function") {
|
|
419
|
+
try {
|
|
420
|
+
const data = parameter();
|
|
421
|
+
return Object.assign([data, null], { data, error: null });
|
|
422
|
+
} catch (error) {
|
|
423
|
+
return Object.assign([null, error], { data: null, error });
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
throw new TypeError("参数类型错误,应为 Promise 或函数");
|
|
427
|
+
};
|
package/dist/esm/index.js
CHANGED
|
@@ -363,10 +363,26 @@ class CubicBezier {
|
|
|
363
363
|
}
|
|
364
364
|
}
|
|
365
365
|
}
|
|
366
|
+
// src/error.ts
|
|
367
|
+
var tryCatch = (parameter) => {
|
|
368
|
+
if (parameter instanceof Promise) {
|
|
369
|
+
return parameter.then((data) => Object.assign([data, null], { data, error: null })).catch((error) => Object.assign([null, error], { data: null, error }));
|
|
370
|
+
}
|
|
371
|
+
if (typeof parameter === "function") {
|
|
372
|
+
try {
|
|
373
|
+
const data = parameter();
|
|
374
|
+
return Object.assign([data, null], { data, error: null });
|
|
375
|
+
} catch (error) {
|
|
376
|
+
return Object.assign([null, error], { data: null, error });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
throw new TypeError("参数类型错误,应为 Promise 或函数");
|
|
380
|
+
};
|
|
366
381
|
export {
|
|
367
382
|
waitSync,
|
|
368
383
|
wait,
|
|
369
384
|
uri,
|
|
385
|
+
tryCatch,
|
|
370
386
|
throttle,
|
|
371
387
|
sleepAsync,
|
|
372
388
|
sleep,
|
package/dist/types/error.d.ts
CHANGED
|
@@ -1 +1,39 @@
|
|
|
1
|
+
type Success<T> = {
|
|
2
|
+
data: T;
|
|
3
|
+
error: null;
|
|
4
|
+
} & [
|
|
5
|
+
data: T,
|
|
6
|
+
error: null
|
|
7
|
+
];
|
|
8
|
+
type Failure<E> = {
|
|
9
|
+
data: null;
|
|
10
|
+
error: E;
|
|
11
|
+
} & [
|
|
12
|
+
data: null,
|
|
13
|
+
error: E
|
|
14
|
+
];
|
|
15
|
+
type Result<T, E extends unknown = Error> = Success<T> | Failure<E>;
|
|
16
|
+
interface tryCatch {
|
|
17
|
+
<T>(arg: Promise<T>): Promise<Result<T>>;
|
|
18
|
+
<V>(arg: (() => V)): Result<V>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 尝试执行一个函数或Promise,返回一个包含执行结果或错误信息的复合对象
|
|
22
|
+
*
|
|
23
|
+
* 该对象同时支持对象属性访问(.data/.error)和数组索引访问([0]/[1])两种方式
|
|
24
|
+
*
|
|
25
|
+
* @param parameter 一个函数或者Promise对象
|
|
26
|
+
* @returns 返回参数中,data和[0]是函数的返回值或是Promise的解析结果,error和[1]是错误对象。两者将有一为`null`
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* tryCatch(() => func(...))
|
|
30
|
+
* // => [data, error] & {data, error}
|
|
31
|
+
* // data: func的返回值,error:函数执行过程中发生的错误
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* await tryCatch(asyncFunc(...))
|
|
35
|
+
* // => [data, error] & {data, error}
|
|
36
|
+
* // data: asyncFunc的返回值,error:asyncFunc执行过程中发生的错误
|
|
37
|
+
*/
|
|
38
|
+
export declare const tryCatch: tryCatch;
|
|
1
39
|
export {};
|