Skip to content

Commit cdfd2b3

Browse files
committed
fix: try-to-catch: ERR_REQUIRE_CYCLE_MODULE
1 parent 4f9ffa6 commit cdfd2b3

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/try-to-catch.cjs

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

3-
const {tryToCatch} = require('./try-to-catch.js');
3+
const isFn = (a) => typeof a === 'function';
4+
5+
const tryToCatch = async (fn, ...args) => {
6+
check(fn);
7+
8+
try {
9+
return [null, await fn(...args)];
10+
} catch(e) {
11+
return [e];
12+
}
13+
};
14+
15+
function check(fn) {
16+
if (!isFn(fn))
17+
throw Error('fn should be a function!');
18+
}
419

520
module.exports = tryToCatch;
621
module.exports.tryToCatch = tryToCatch;

test/try-to-catch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {promisify} from 'node:util';
22
import test from 'supertape';
3-
import {tryToCatch} from 'try-to-catch';
3+
import tryToCatchDefault, {tryToCatch} from 'try-to-catch';
44

55
test('try-to-catch: no args', async (t) => {
66
const [e] = await tryToCatch(tryToCatch);
@@ -67,7 +67,7 @@ test('try-to-catch: resolves: promisify', async (t) => {
6767

6868
test('try-to-catch: nested', async (t) => {
6969
const fn = () => 5;
70-
const [, data] = await tryToCatch.tryToCatch(fn);
70+
const [, data] = await tryToCatchDefault(fn);
7171

7272
t.equal(data, 5, 'should not be error');
7373
t.end();

0 commit comments

Comments
 (0)