1
1
# Using Try Catch
2
2
3
- Simplify the use of try-catch
3
+ Simplify the use of try-catch.
4
+ Avoid writing code that contains high scope decoupling with using-try-catch.
4
5
5
6
![ Main Branch] ( https://github.com/oda2/using-try-catch/actions/workflows/main.yml/badge.svg?branch=main )
6
7
[ ![ Codacy Badge] ( https://api.codacy.com/project/badge/Grade/46b647bc48d54dc09d0b31e84fa2644f )] ( https://app.codacy.com/gh/Oda2/using-try-catch?utm_source=github.com&utm_medium=referral&utm_content=Oda2/using-try-catch&utm_campaign=Badge_Grade_Settings )
7
8
[ ![ Coverage Status] ( https://coveralls.io/repos/github/Oda2/using-try-catch/badge.svg )] ( https://coveralls.io/github/Oda2/using-try-catch )
8
9
[ ![ GitHub license] ( https://img.shields.io/github/license/Oda2/using-try-catch )] ( https://github.com/Oda2/using-try-catch/blob/master/LICENSE )
9
10
[ ![ GitHub issues] ( https://img.shields.io/github/issues/Oda2/using-try-catch )] ( https://github.com/Oda2/using-try-catch/issues )
10
11
[ ![ GitHub stars] ( https://img.shields.io/github/stars/Oda2/using-try-catch )] ( https://github.com/Oda2/using-try-catch/stargazers )
12
+ [ ![ CDN jsdelivr
] ( https://img.shields.io/badge/cdn%20jsdelivr-0.1.5-green )] ( https://cdn.jsdelivr.net/npm/[email protected] /dist/index.js )
13
+ [ ![ NPM Size] ( https://img.shields.io/bundlephobia/min/using-try-catch )] ( https://www.npmjs.com/package/using-try-catch )
14
+ [ ![ Vulnerability] ( https://img.shields.io/snyk/vulnerabilities/github/oda2/using-try-catch )] ( https://github.com/Oda2/using-try-catch )
11
15
12
16
## Installation
13
17
@@ -23,7 +27,9 @@ $ yarn add using-try-catch
23
27
$ pnpm add using-try-catch
24
28
```
25
29
26
- ### Examples
30
+ ## Examples
31
+
32
+ ### Typescript
27
33
28
34
``` js
29
35
import usingTryCatch from ' using-try-catch' ;
@@ -38,18 +44,35 @@ const example = async () => {
38
44
example ();
39
45
```
40
46
47
+ ### CommonJS
48
+
49
+ ``` js
50
+ const usingTryCatch = require (' using-try-catch' );
51
+
52
+ const example = async () => {
53
+ const promise = new Promise ((resolve ) => resolve (' exemple' ));
54
+
55
+ const result = await usingTryCatch (promise);
56
+ console .log (result .data ); // 'example'
57
+ };
58
+
59
+ example ();
60
+ ```
61
+
41
62
### Browser Examples
42
63
43
64
``` html
44
65
<!DOCTYPE html>
45
66
<html >
46
67
<head >
47
- <
script src =
" https://cdn.jsdelivr.net/npm/[email protected] " ></
script >
68
+ <meta charset =" utf-8" >
69
+ <meta content =" width=device-width, initial-scale=1" name =" viewport" >
70
+ <title >Example using-try-catch</title >
48
71
</head >
49
72
<body >
73
+ <h1 >Example</h1 >
50
74
51
- <h1 >Exemplo</h1 >
52
-
75
+ <
script type =
" text/javascript" src =
" https://cdn.jsdelivr.net/npm/[email protected] " ></
script >
53
76
<script >
54
77
document .addEventListener (' DOMContentLoaded' , function loaded () {
55
78
@@ -67,6 +90,60 @@ example();
67
90
</html >
68
91
```
69
92
93
+ ## The problem
94
+
95
+ Several times we need to scope our async/await as follows:
96
+
97
+ ``` js
98
+ const example = async () => {
99
+ let promise1;
100
+ let promise2;
101
+ let err = false ;
102
+
103
+ try {
104
+ promise1 = await new Promise ((resolve ) => resolve (' exemple 1' ));
105
+ } catch {
106
+ err = true ;
107
+ }
108
+
109
+ try {
110
+ promise2 = await new Promise ((resolve ) => resolve (' exemple 2' ));
111
+ } catch {
112
+ err = true ;
113
+ }
114
+
115
+ if (err) {
116
+ return ' Boom'
117
+ }
118
+
119
+ return {
120
+ text1: promise1,
121
+ text2: promise2
122
+ }
123
+ };
124
+
125
+ example ();
126
+ ```
127
+
128
+ With using-try-catch we can simplify this operation as follows
129
+
130
+ ``` js
131
+ const example = async () => {
132
+ const promise1 = await usingTryCatch (await new Promise ((resolve ) => resolve (' exemple 1' )));
133
+ const promise2 = await usingTryCatch (await new Promise ((resolve ) => resolve (' exemple 2' )));
134
+
135
+ if (promise1 .err || promise2 .err ) {
136
+ return ' Boom' ;
137
+ }
138
+
139
+ return {
140
+ text1: promise1 .data ,
141
+ text2: promise2 .data
142
+ }
143
+ };
144
+
145
+ example ();
146
+ ```
70
147
71
148
### NPM Statistics
72
149
0 commit comments