Skip to content

Commit 294f665

Browse files
committed
feature(try-to-catch) add support of a functions
1 parent 6ba7260 commit 294f665

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ Wrap function to avoid `try-catch` block, resolves `[error, result]`;
2626

2727
### Example
2828

29+
Simplest example with `async-await`:
30+
31+
```js
32+
const tryToCatch = require('try-to-catch');
33+
await tryToCatch(Promise.reject('hi'));
34+
// returns
35+
[ Error: hi]
36+
```
37+
38+
Can be used with functions:
39+
40+
```js
41+
const tryToCatch = require('try-to-catch');
42+
await tryToCatch(() => 5);
43+
// returns
44+
[null, 5]
45+
```
46+
47+
Advanced example:
48+
2949
```js
3050
const fs = require('fs');
3151
const tryToCatch = require('try-to-catch');
@@ -60,7 +80,7 @@ var tryToCatch = require('try-to-catch/legacy');
6080

6181
## Related
6282

63-
- [try-catch](https://github.com/coderaiser/try-catch "TryCatch") - functional try-catch wrapper.
83+
- [try-catch](https://github.com/coderaiser/try-catch "try-catch") - functional try-catch wrapper.
6484

6585
## License
6686

lib/try-to-catch.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
'use strict';
22

3+
const wraptile = require('wraptile/legacy');
4+
35
const success = (a) => [null, a];
46
const fail = (a) => [a];
57

68
module.exports = (fn, ...args) => {
79
check(fn);
810

9-
return fn(...args)
11+
return Promise.resolve()
12+
.then(wrap(fn, args))
1013
.then(success)
1114
.catch(fail);
1215
};
1316

17+
function wrap(fn, args) {
18+
if (!args.length)
19+
return fn;
20+
21+
return wraptile(fn, ...args);
22+
}
23+
1424
function check(fn) {
1525
if (typeof fn !== 'function')
1626
throw Error('fn should be a function!');

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
"wisdom": "redrun build legacy",
2020
"legacy": "echo \"module.exports = require('./try-to-catch')\" > legacy/index.js"
2121
},
22-
"dependencies": {},
22+
"dependencies": {
23+
"wraptile": "^2.0.0"
24+
},
2325
"keywords": [
2426
"try",
2527
"catch",

test/try-to-catch.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,31 @@ test('try-to-catch: rejects', async (t) => {
3030
t.end();
3131
});
3232

33+
test('try-to-catch: rejects: not promise', async (t) => {
34+
const message = 'hello';
35+
const fn = (a) => {
36+
throw Error(a);
37+
};
38+
39+
const [error] = await tryToCatch(fn, message);
40+
41+
t.equal(error.message, message, 'should equal');
42+
t.end();
43+
});
44+
45+
test('try-to-catch: resolves: not promise', async (t) => {
46+
const fn = () => {};
47+
const [error] = await tryToCatch(fn);
48+
49+
t.notOk(error, 'should not be error');
50+
t.end();
51+
});
52+
53+
test('try-to-catch: resolves: not promise', async (t) => {
54+
const fn = () => 5;
55+
const [, data] = await tryToCatch(fn);
56+
57+
t.equal(data, 5, 'should not be error');
58+
t.end();
59+
});
60+

0 commit comments

Comments
 (0)