From: Jérôme Benoit Date: Wed, 16 Oct 2024 15:17:24 +0000 (+0200) Subject: perf: speed up isAsyncFunction() helper X-Git-Tag: ocpp-server@v1.5.2~11 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f8d74e92b50b36d7e5cb9c4a8ecbfcfc8fee0cee;p=e-mobility-charging-stations-simulator.git perf: speed up isAsyncFunction() helper Signed-off-by: Jérôme Benoit --- 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 => {