@nannygoose-utils/async-handler 1.0.0
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/index.d.ts +16 -0
- package/dist/index.mjs +60 -0
- package/package.json +23 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 处理异步 Promise 操作的错误
|
|
3
|
+
* @param promise 要执行的异步 Promise 操作
|
|
4
|
+
* @returns 数组 [error, result]:成功时 error 为 null,失败时 result 为 undefined
|
|
5
|
+
*/
|
|
6
|
+
export declare function to<T>(promise: Promise<T>): Promise<[Error | null, T | undefined]>;
|
|
7
|
+
/**
|
|
8
|
+
* 扩展:处理同步函数的错误(保持和 to 函数一致的返回格式)
|
|
9
|
+
* @param fn 要执行的同步函数
|
|
10
|
+
* @returns 数组 [error, result]
|
|
11
|
+
* @example
|
|
12
|
+
* const [err, parsedData] = toSync(() => JSON.parse('{"name":"test"}'));
|
|
13
|
+
*/
|
|
14
|
+
export declare function toSync<T>(fn: () => T): [Error | null, T | undefined];
|
|
15
|
+
export type AsyncResult<T> = [Error | null, T | undefined];
|
|
16
|
+
export type SyncResult<T> = [Error | null, T | undefined];
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* 标准化错误对象
|
|
12
|
+
* @param error 捕获到的任意类型错误
|
|
13
|
+
* @returns 标准 Error 对象
|
|
14
|
+
*/
|
|
15
|
+
function normalizeError(error) {
|
|
16
|
+
// 如果已经是 Error 实例,直接返回
|
|
17
|
+
if (error instanceof Error) {
|
|
18
|
+
return error;
|
|
19
|
+
}
|
|
20
|
+
// 处理对象类型的错误(如 { message: 'xxx' })
|
|
21
|
+
if (error instanceof Object) {
|
|
22
|
+
return new Error(JSON.stringify(error));
|
|
23
|
+
}
|
|
24
|
+
// 处理字符串/数字等基础类型错误
|
|
25
|
+
return new Error(String(error));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 处理异步 Promise 操作的错误
|
|
29
|
+
* @param promise 要执行的异步 Promise 操作
|
|
30
|
+
* @returns 数组 [error, result]:成功时 error 为 null,失败时 result 为 undefined
|
|
31
|
+
*/
|
|
32
|
+
export function to(promise) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
try {
|
|
35
|
+
const result = yield promise;
|
|
36
|
+
return [null, result];
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
const normalizedError = normalizeError(error);
|
|
40
|
+
return [normalizedError, undefined];
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 扩展:处理同步函数的错误(保持和 to 函数一致的返回格式)
|
|
46
|
+
* @param fn 要执行的同步函数
|
|
47
|
+
* @returns 数组 [error, result]
|
|
48
|
+
* @example
|
|
49
|
+
* const [err, parsedData] = toSync(() => JSON.parse('{"name":"test"}'));
|
|
50
|
+
*/
|
|
51
|
+
export function toSync(fn) {
|
|
52
|
+
try {
|
|
53
|
+
const result = fn();
|
|
54
|
+
return [null, result];
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
const normalizedError = normalizeError(error);
|
|
58
|
+
return [normalizedError, undefined];
|
|
59
|
+
}
|
|
60
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nannygoose-utils/async-handler",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "异步/同步错误处理工具,统一返回 [error, result] 格式",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc && node -e \"require('fs').renameSync('dist/index.js', 'dist/index.mjs')\"",
|
|
14
|
+
"prepublishOnly": "npm run build"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "<tandongyang@1ceng.com>",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^25.2.0",
|
|
21
|
+
"typescript": "^5.9.3"
|
|
22
|
+
}
|
|
23
|
+
}
|