66[ ![ dependency-review] ( https://github.com/FrontEndDev-org/try-flatten/actions/workflows/dependency-review.yml/badge.svg )] ( https://github.com/FrontEndDev-org/try-flatten/actions/workflows/dependency-review.yml )
77[ ![ Codacy Badge] ( https://app.codacy.com/project/badge/Grade/948a21cc839b431490dd8b8bf22628c3 )] ( https://app.codacy.com/gh/FrontEndDev-org/try-flatten/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade )
88[ ![ Codacy Badge] ( https://app.codacy.com/project/badge/Coverage/948a21cc839b431490dd8b8bf22628c3 )] ( https://app.codacy.com/gh/FrontEndDev-org/try-flatten/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage )
9- [ ![ npm] ( https://img.shields.io/npm/v/try-flatten )] ( https://npmjs.com/package/try-flatten )
10- [ ![ release] ( https://img.shields.io/github/v/release/FrontEndDev-org/try-flatten )] ( https://github.com/FrontEndDev-org/try-flatten/releases )
9+ [ ![ npm version] ( https://badge.fury.io/js/try-flatten.svg )] ( https://npmjs.com/package/try-flatten )
1110
12- # 为什么需要这个
1311
12+ # 为什么需要这个
1413## try-catch 块级作用域带来的问题
15-
1614先看一段代码:
1715
1816``` ts
@@ -64,8 +62,10 @@ if (res) {
6462可以看到,由于块级作用域的特性,导致 res 的类型被”污染“了, 使用 try-flatten 后,你将可以用一种“扁平化”的方式调用 try-catch, 不用为了类型安全写一些冗余代码。
6563
6664## 用上 ` try-flatten ` 后
67-
6865``` ts
66+ import { tryFlatten } from ' try-flatten' ;
67+
68+ const somePromise = Promise .resolve ({ prop: ' value' });
6969const [err, res] = await tryFlatten (somePromise );
7070
7171// 只需要判断 err 是否存在即可
@@ -82,18 +82,23 @@ console.log(res.prop); // 'value'
8282```
8383
8484# 下载安装
85-
8685``` shell
8786npm install try-flatten
8887```
8988
9089## 在线试用
90+ [ tryFlatten TypeScript Playground] ( https://amz.fun/NRh1L )
9191
92- [ Playground Link] ( https://www.typescriptlang.org/zh/play?#code/JYWwDg9gTgLgBAbzjKBPAYgGwIYxgUwDs4BfOAMyghDgHIVUBacnPI2gbgCgv8APSLAoBXQgGMYwCMWwBnWflgA5YZkwAKAG7ZMw-AC44hVZgCUiLnDhR8MYVGLbd+OAF53Rk9xI9+g+OSiElIy8oowAKqEACb45MCE+NFaOnqGorHxidHmCJbWtvaOqS7urnAZcQlJ3r4C0AFBktJwcgqwAKJQVFApzoZdPbn5NnYOcE56cABk0xMlcAmyMNji+BDkcIPQtbz1QoHizaHtMCogAEaKfWmel4rDVqNFyKhg65uTpR60xvdQnC4Pj2-hERxCyHwy3UhGwIAMcGWUASAHMADSQ5YAYTkCPU5lcAD5WoRUI84GJpLIIJh8AA6TAQFHqehQySEFF0rm0DGw+GmbhWAjY3H43bCmAswAwKoBTa0Av4qAB1MeXB8W5iXkrJTCMs4ABtRRQDE2WQAXTcyDQWFwBEI6lVRLgAEYBTwrMBNuoDeSrG1wttel7BVZWmFYFFMtVksaXVYyPhMAoLMGQ6cVGpPd0Y8HfcphP91NGgz4SC6uBKWYA9tUAwDGK5VyVDiFUE9X5LU6sQ6TAXbBiADW6CaEPK6g7am7fcMGagA260DgAB9PGojVDDH8rlBmxMIMAcmqk8GFDAACqgdbCSX2lvJzWd8e9mEmDHOoOxrMkINt+D67or03m7AAHdsGAeAGCtNhbVHLse37QdpFLN0PS9A8fVDGB-SnLM0NOcMqmyAsoXfOB40TDVs3QtMNEDfIcPCc4N0I2R3yBUty1oAAFKgQGABQABlgF7fA61kBsxCbfdyK-PUDT-M1yiAkCwMtVgbXULjqF4+ljRpTR8HUZ1EMWZDM1QlM-VnANM1fcywxifCkiY4jSJcci6NzdMaOTHMzjzRjC3yYsXSAA )
9392
94- # 对同步函数的 try-flatten
93+ # 使用方法
94+ ## 对同步函数的 try-flatten
9595
9696``` ts
97+ import { tryFunction , tryFlatten } from ' try-flatten' ;
98+
99+ // 推荐使用 tryFunction
100+ const [err, res] = tryFunction (() => 1 );
101+ // 与 tryFunction 同价
97102const [err, res] = tryFlatten (() => 1 );
98103
99104// 只需要判断 err 是否存在即可
@@ -109,12 +114,20 @@ console.log(err === null);
109114console .log (res === 1 );
110115```
111116
112- # 对回调函数的 try-flatten
117+ ## 对回调函数的 try-flatten
118+ ### 情况 1:没有入参
113119
114120``` ts
115- const [err, res] = await tryFlatten ((callback : (err : Error | null , res : number ) => void ) => {
116- callback (null , 1 );
117- });
121+ import { type Callback , tryCallback , tryFlatten } from ' try-flatten' ;
122+
123+ const cf = (cb : Callback <number >) => {
124+ cb (null , 1 );
125+ };
126+
127+ // 推荐使用 tryCallback
128+ const [err, res] = await tryCallback (cf );
129+ // 与 tryCallback 等价
130+ const [err, res] = await tryFlatten (cf );
118131
119132// 只需要判断 err 是否存在即可
120133if (err ) {
@@ -129,9 +142,40 @@ console.log(err === null);
129142console .log (res === 1 );
130143```
131144
132- # 对 PromiseLike 的 try-flatten
145+ ### 情况 2:有其他入参
146+ ``` ts
147+ import { type Callback , callbackCurry , tryCallback , tryFlatten } from ' try-flatten' ;
148+
149+ const cf = (a : number , b : number , cb : Callback <number >) => {
150+ cb (null , a + b + 1 );
151+ };
152+
153+ // 推荐使用 tryCallback,不需要额外的 callbackCurry 辅助
154+ const [err, res] = await tryCallback (cf , 1 , 2 );
155+ // 与 tryCallback 等价
156+ const [err, res] = await tryFlatten (callbackCurry (cf , 1 , 2 ));
157+
158+ // 只需要判断 err 是否存在即可
159+ if (err ) {
160+ // 此处 err 类型为 Error,res 类型为 undefined
161+ console .log (err instanceof Error );
162+ console .log (res === undefined );
163+ return ;
164+ }
165+
166+ // 此处 err 类型为 null,res 类型为 number
167+ console .log (err === null );
168+ console .log (res === 4 );
169+ ```
170+
171+ ## 对 PromiseLike 的 try-flatten
133172
134173``` ts
174+ import { tryPromise , tryFlatten } from ' try-flatten' ;
175+
176+ // 推荐使用 tryPromise
177+ const [err, res] = await tryPromise (Promise .resolve (1 ));
178+ // 与 tryPromise 等价
135179const [err, res] = await tryFlatten (Promise .resolve (1 ));
136180
137181// 只需要判断 err 是否存在即可
@@ -148,5 +192,4 @@ console.log(res === 1);
148192```
149193
150194# 启发
151-
152195- < https://www.npmjs.com/package/flatry >
0 commit comments