Skip to content

Commit fefe27f

Browse files
committed
docs: Update README.md
1 parent b09cb8a commit fefe27f

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,4 @@ jobs:
2828
- run: npm publish
2929
env:
3030
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
31-
32-
github:
33-
runs-on: ubuntu-latest
34-
steps:
35-
- name: Checkout repo
36-
uses: actions/checkout@v2
37-
38-
- name: Install deps and build (with cache)
39-
uses: pnpm/[email protected]
40-
with:
41-
version: 6.6.2
42-
run_install: true
43-
registry: https://npm.pkg.github.com
4431

45-
- name: Build
46-
run: pnpm build
47-
48-
- uses: actions/setup-node@v1
49-
with:
50-
node-version: 14
51-
registry-url: https://npm.pkg.github.com
52-
scope: '@oda2'
53-
always-auth: true
54-
55-
- run: npm publish
56-
env:
57-
NODE_AUTH_TOKEN: ${{secrets.NPM_GITHUB_TOKEN}}

README.md

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
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
2935
import usingTryCatch from 'using-try-catch';
@@ -38,18 +44,35 @@ const example = async () => {
3844
example();
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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.4",
2+
"version": "0.1.5",
33
"license": "MIT",
44
"name": "using-try-catch",
55
"homepage": "https://github.com/Oda2/using-try-catch",

0 commit comments

Comments
 (0)