Error handling and unit tests
[poolifier.git] / lib / fixed.js
index 645528283d80807cbc5374b5184c64b0f4c83c9e..8d899d57bb4ed00a54e4d1a1f2093641d8bd3f77 100644 (file)
@@ -4,6 +4,7 @@ const {
 } = require('worker_threads')
 
 function empty () {}
+const _void = {}
 /**
  * A thread pool with a static number of threads , is possible to execute tasks in sync or async mode as you prefer. <br>
  * This pool will select the worker thread in a round robin fashion. <br>
@@ -50,7 +51,7 @@ class FixedThreadPool {
     this.tasks.set(worker, this.tasks.get(worker) + 1)
     const id = ++this._id
     const res = this._execute(worker, id)
-    worker.postMessage({ data: data, _id: id })
+    worker.postMessage({ data: data || _void, _id: id })
     return res
   }
 
@@ -60,7 +61,8 @@ class FixedThreadPool {
         if (message._id === id) {
           worker.port2.removeListener('message', listener)
           this.tasks.set(worker, this.tasks.get(worker) - 1)
-          resolve(message.data)
+          if (message.error) reject(message.error)
+          else resolve(message.data)
         }
       }
       worker.port2.on('message', listener)