Skip to content

Commit ff2fa0a

Browse files
committed
test: Add more test with promisse Array
1 parent 428b049 commit ff2fa0a

File tree

6 files changed

+89
-31
lines changed

6 files changed

+89
-31
lines changed

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"plugin:@typescript-eslint/eslint-recommended",
1111
"plugin:@typescript-eslint/recommended",
1212
"plugin:prettier/recommended"
13-
]
13+
],
14+
"rules": {
15+
"@typescript-eslint/no-non-null-assertion": "off"
16+
}
1417
}

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# Using Try Catch
23

34
Simplify the use of try-catch.
@@ -12,6 +13,7 @@ Avoid writing code that contains high scope decoupling with using-try-catch.
1213
[![CDN jsdelivr](https://img.shields.io/badge/cdn%20jsdelivr-0.1.5-green)](https://cdn.jsdelivr.net/npm/[email protected]/dist/index.js)
1314
[![NPM Size](https://img.shields.io/bundlephobia/min/using-try-catch)](https://www.npmjs.com/package/using-try-catch)
1415
[![Vulnerability](https://img.shields.io/snyk/vulnerabilities/github/oda2/using-try-catch)](https://github.com/Oda2/using-try-catch)
16+
1517
[![Edit admiring-sun-5qry6](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/using-try-catch-zul50)
1618

1719
## Installation
@@ -91,6 +93,46 @@ example();
9193
</html>
9294
```
9395

96+
### Fetch Example
97+
98+
```js
99+
const https = require('https');
100+
const { usingTryCatch } = require('using-try-catch');
101+
102+
const fetchDog = () => new Promise((resolve, reject) => {
103+
const options = {
104+
host: 'dog.ceo', //Xdog.ceo
105+
path: '/api/breeds/image/random',
106+
method: 'GET',
107+
port: 443
108+
};
109+
110+
const request = https.request(options, (res) => {
111+
res.setEncoding('utf-8');
112+
113+
let body = '';
114+
res.on('data', (chunk) => body += chunk);
115+
res.on('end', () => resolve(JSON.parse(body)));
116+
res.on('error', (error) => reject(error));
117+
});
118+
119+
request.on('error', (error) => reject(`Error in request: ${error}`));
120+
request.end();
121+
});
122+
123+
const fetchData = async () => {
124+
const { data, error } = await usingTryCatch(fetchDog());
125+
126+
if (error) {
127+
return console.log('Error => ', error); // Error in request: Error: getaddrinfo ENOTFOUND Xdog.ceo
128+
}
129+
130+
return console.log('Data => ', data); // { message: 'https://images.dog.ceo/breeds/terrier-fox/n02095314_3189.jpg', status: 'success' }
131+
};
132+
133+
fetchData();
134+
```
135+
94136
## The problem
95137

96138
Several times we need to scope our async/await as follows:

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"build:umd": "rollup -c rollup.config.ts",
3333
"test": "jest .",
3434
"coverage": "jest . --coverage",
35-
"lint": "eslint ."
35+
"lint": "eslint .",
36+
"postbuild": "cp package.json dist"
3637
},
3738
"devDependencies": {
3839
"@rollup/plugin-typescript": "^8.2.1",
@@ -51,7 +52,7 @@
5152
"rollup-plugin-cleanup": "^3.2.1",
5253
"ts-jest": "26.5.5",
5354
"tslib": "^2.2.0",
54-
"typescript": "^4.2.4"
55+
"typescript": "^4.3.2"
5556
},
5657
"keywords": [
5758
"try-catch",

pnpm-lock.yaml

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { default } from './using-try-catch';
2-

test/usingTryCatch.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,17 @@ describe('using Try Catch', () => {
3535
expect(result.data![3]).toEqual('ola 4');
3636
expect(result.data![4]).toEqual('ola 5');
3737
});
38+
39+
it('Return Error after execution with Array Promisse', async () => {
40+
const promisses = [];
41+
promisses.push(new Promise<string>((resolve) => resolve('ola 1')));
42+
promisses.push(new Promise<string>((resolve) => resolve('ola 2')));
43+
promisses.push(new Promise<string>((_, reject) => reject('Boom')));
44+
promisses.push(new Promise<string>((resolve) => resolve('ola 4')));
45+
promisses.push(new Promise<string>((resolve) => resolve('ola 5')));
46+
47+
const result = await usingTryCatch<string>(promisses);
48+
expect(result.data).toBeNull();
49+
expect(result.error).toEqual('Boom');
50+
});
3851
});

0 commit comments

Comments
 (0)