Skip to content

Commit

Permalink
perf: speed up isAsyncFunction() helper
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed Oct 16, 2024
1 parent 7cd7142 commit f8d74e9
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,17 @@ export const clone = <T>(object: T): T => {
return structuredClone<T>(object)
}

type AsyncFunctionType<A extends unknown[], R> = (...args: A) => PromiseLike<R>

/**
* 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<unknown> => {
return is(Function, fn) && fn.constructor.name === 'AsyncFunction'
export const isAsyncFunction = (fn: unknown): fn is AsyncFunctionType<unknown[], unknown> => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return fn?.constructor === (async () => {}).constructor
}

export const isObject = (value: unknown): value is object => {
Expand Down

0 comments on commit f8d74e9

Please sign in to comment.