From f8d74e92b50b36d7e5cb9c4a8ecbfcfc8fee0cee Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 16 Oct 2024 17:17:24 +0200 Subject: [PATCH] perf: speed up isAsyncFunction() helper MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 2da9b1cb..56b528a6 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -208,14 +208,17 @@ export const clone = (object: T): T => { return structuredClone(object) } +type AsyncFunctionType = (...args: A) => PromiseLike + /** * Detects whether the given value is an asynchronous function or not. * @param fn - Unknown value. * @returns `true` if `fn` was an asynchronous function, otherwise `false`. * @internal */ -export const isAsyncFunction = (fn: unknown): fn is (...args: unknown[]) => Promise => { - return is(Function, fn) && fn.constructor.name === 'AsyncFunction' +export const isAsyncFunction = (fn: unknown): fn is AsyncFunctionType => { + // eslint-disable-next-line @typescript-eslint/no-empty-function + return fn?.constructor === (async () => {}).constructor } export const isObject = (value: unknown): value is object => { -- 2.34.1