Apply dependencies update (#359)
[poolifier.git] / src / worker / abstract-worker.ts
index d41115bc390b26f63eeaa9ae4500d678cb1bd7b4..26468aeb96ae0d5f838518e2e9308b2e31c16c0e 100644 (file)
@@ -2,6 +2,7 @@ import { AsyncResource } from 'async_hooks'
 import type { Worker } from 'cluster'
 import type { MessagePort } from 'worker_threads'
 import type { MessageValue } from '../utility-types'
+import { EMPTY_FUNCTION } from '../utils'
 import type { KillBehavior, WorkerOptions } from './worker-options'
 import { KillBehaviors } from './worker-options'
 
@@ -101,7 +102,7 @@ export abstract class AbstractWorker<
    *
    * @param fn The function that should be defined.
    */
-  private checkFunctionInput (fn: (data: Data) => Response) {
+  private checkFunctionInput (fn: (data: Data) => Response): void {
     if (!fn) throw new Error('fn parameter is mandatory')
   }
 
@@ -156,10 +157,10 @@ export abstract class AbstractWorker<
     try {
       const res = fn(value.data)
       this.sendToMainWorker({ data: res, id: value.id })
-      this.lastTask = Date.now()
     } catch (e) {
       const err = this.handleError(e)
       this.sendToMainWorker({ error: err, id: value.id })
+    } finally {
       this.lastTask = Date.now()
     }
   }
@@ -177,13 +178,15 @@ export abstract class AbstractWorker<
     fn(value.data)
       .then(res => {
         this.sendToMainWorker({ data: res, id: value.id })
-        this.lastTask = Date.now()
         return null
       })
       .catch(e => {
         const err = this.handleError(e)
         this.sendToMainWorker({ error: err, id: value.id })
+      })
+      .finally(() => {
         this.lastTask = Date.now()
       })
+      .catch(EMPTY_FUNCTION)
   }
 }