Skip to content

Commit 30fca7c

Browse files
committed
feature(try-to-catch) promise -> async
1 parent 10de684 commit 30fca7c

File tree

4 files changed

+9
-21
lines changed

4 files changed

+9
-21
lines changed

.babelrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/try-to-catch.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
'use strict';
22

3-
const success = (a) => [null, a];
4-
const fail = (a) => [a];
5-
6-
const noArg = (f, a) => () => f(...a);
7-
8-
module.exports = (fn, ...args) => {
3+
module.exports = async (fn, ...args) => {
94
check(fn);
105

11-
return Promise.resolve()
12-
.then(noArg(fn, args))
13-
.then(success)
14-
.catch(fail);
6+
try {
7+
return [null, await fn(...args)];
8+
} catch(e) {
9+
return [e];
10+
}
1511
};
1612

1713
function check(fn) {

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
"then"
3030
],
3131
"devDependencies": {
32-
"@babel/cli": "^7.0.0",
33-
"@babel/core": "^7.0.0",
34-
"@babel/preset-env": "^7.0.0",
3532
"coveralls": "^3.0.0",
3633
"eslint": "^6.1.0",
3734
"eslint-plugin-node": "^11.0.0",

test/try-to-catch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const {promisify} = require('util');
44
const test = require('supertape');
55
const tryToCatch = require('..');
66

7-
test('try-to-catch: no args', (t) => {
8-
const fn = () => tryToCatch();
7+
test('try-to-catch: no args', async (t) => {
8+
const [e] = await tryToCatch(tryToCatch);
99

10-
t.throws(fn, /fn should be a function!/, 'should throw');
10+
t.equal(e.message, 'fn should be a function!', 'should throw');
1111
t.end();
1212
});
1313

0 commit comments

Comments
 (0)