11# Using Try Catch
22
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.
45
56![ Main Branch] ( https://github.com/oda2/using-try-catch/actions/workflows/main.yml/badge.svg?branch=main )
67[ ![ 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 )
78[ ![ Coverage Status] ( https://coveralls.io/repos/github/Oda2/using-try-catch/badge.svg )] ( https://coveralls.io/github/Oda2/using-try-catch )
89[ ![ GitHub license] ( https://img.shields.io/github/license/Oda2/using-try-catch )] ( https://github.com/Oda2/using-try-catch/blob/master/LICENSE )
910[ ![ GitHub issues] ( https://img.shields.io/github/issues/Oda2/using-try-catch )] ( https://github.com/Oda2/using-try-catch/issues )
1011[ ![ 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 )
1115
1216## Installation
1317
@@ -23,7 +27,9 @@ $ yarn add using-try-catch
2327$ pnpm add using-try-catch
2428```
2529
26- ### Examples
30+ ## Examples
31+
32+ ### Typescript
2733
2834``` js
2935import usingTryCatch from ' using-try-catch' ;
@@ -38,18 +44,35 @@ const example = async () => {
3844example ();
3945```
4046
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+
4162### Browser Examples
4263
4364``` html
4465<!DOCTYPE html>
4566<html >
4667 <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 >
4871 </head >
4972 <body >
73+ <h1 >Example</h1 >
5074
51- <h1 >Exemplo</h1 >
52-
75+ <
script type =
" text/javascript" src =
" https://cdn.jsdelivr.net/npm/[email protected] " ></
script >
5376 <script >
5477 document .addEventListener (' DOMContentLoaded' , function loaded () {
5578
@@ -67,6 +90,60 @@ example();
6790</html >
6891```
6992
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+ ```
70147
71148### NPM Statistics
72149
0 commit comments