): value is KB => {
return value === killBehavior
}
+
+/**
+ * Detects whether the given value is an asynchronous function or not.
+ *
+ * @param fn - Any value.
+ * @returns `true` if `fn` was an asynchronous function, otherwise `false`.
+ */
+export const isAsyncFunction = (
+ fn: unknown
+): fn is (...args: unknown[]) => Promise<unknown> => {
+ return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction'
+}
TaskPerformance,
WorkerStatistics
} from '../utility-types'
-import { EMPTY_FUNCTION, isPlainObject } from '../utils'
+import { EMPTY_FUNCTION, isAsyncFunction, isPlainObject } from '../utils'
import {
type KillBehavior,
KillBehaviors,
if (message.id != null && message.data != null) {
// Task message received
const fn = this.getTaskFunction(message.name)
- if (fn?.constructor.name === 'AsyncFunction') {
+ if (isAsyncFunction(fn)) {
this.runInAsyncScope(this.runAsync.bind(this), this, fn, message)
} else {
this.runInAsyncScope(this.runSync.bind(this), this, fn, message)