From 09fd6c303685b2a7c83b49df74e444b9fbd2844a Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 22 Jan 2025 22:08:55 -0800 Subject: [PATCH] [Refactor] use `async-function` for the eval parts Hopefully fixes #31. --- getAsyncFunc.d.ts | 5 ----- getAsyncFunc.js | 10 ---------- index.d.ts | 6 ++++-- index.js | 13 +++---------- package.json | 1 + 5 files changed, 8 insertions(+), 27 deletions(-) delete mode 100644 getAsyncFunc.d.ts delete mode 100644 getAsyncFunc.js diff --git a/getAsyncFunc.d.ts b/getAsyncFunc.d.ts deleted file mode 100644 index 4095333..0000000 --- a/getAsyncFunc.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import type { AsyncFunction } from '.'; - -declare function getAsyncFunc(): AsyncFunction | false; - -export = getAsyncFunc; \ No newline at end of file diff --git a/getAsyncFunc.js b/getAsyncFunc.js deleted file mode 100644 index f1a79c8..0000000 --- a/getAsyncFunc.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -/** @type {import('./getAsyncFunc')} */ -module.exports = function () { - try { - return Function('return async function () {}')(); - } catch (e) { - return false; - } -}; diff --git a/index.d.ts b/index.d.ts index 74949b0..515f3cb 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,9 @@ +import type { AsyncFunction } from 'async-function'; + declare namespace isAsyncFunction { - type AsyncFunction = (...args: any[]) => Promise + export type { AsyncFunction }; } -declare function isAsyncFunction(fn: unknown): fn is isAsyncFunction.AsyncFunction; +declare function isAsyncFunction(fn: unknown): fn is AsyncFunction; export = isAsyncFunction; \ No newline at end of file diff --git a/index.js b/index.js index cb39b9f..cb39da5 100644 --- a/index.js +++ b/index.js @@ -10,10 +10,7 @@ var isFnRegex = safeRegexTest(/^\s*async(?:\s+function(?:\s+|\()|\s*\()/); var hasToStringTag = require('has-tostringtag/shams')(); var getProto = require('get-proto'); -var getAsyncFunc = require('./getAsyncFunc'); - -/** @type {import('.').AsyncFunction | false} */ -var AsyncFunction; +var getAsyncFunc = require('async-function'); /** @type {import('.')} */ module.exports = function isAsyncFunction(fn) { @@ -30,10 +27,6 @@ module.exports = function isAsyncFunction(fn) { if (!getProto) { return false; } - if (typeof AsyncFunction === 'undefined') { - var asyncFunc = getAsyncFunc(); - // eslint-disable-next-line no-extra-parens - AsyncFunction = asyncFunc ? /** @type {import('.').AsyncFunction} */ (getProto(asyncFunc)) : false; - } - return AsyncFunction && getProto(fn) === AsyncFunction; + var asyncFunc = getAsyncFunc(); + return asyncFunc && asyncFunc.prototype === getProto(fn); }; diff --git a/package.json b/package.json index ca547eb..401e7e0 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "url": "https://github.com/inspect-js/is-async-function/issues" }, "dependencies": { + "async-function": "^1.0.0", "call-bound": "^1.0.3", "get-proto": "^1.0.1", "has-tostringtag": "^1.0.2",