refactor: cleanup promise usage code
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 15 Aug 2023 10:49:15 +0000 (12:49 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 15 Aug 2023 10:49:15 +0000 (12:49 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts

index 2a52d5486984133e620552877e90921d50d96190..f922aff5b3bb801c0e8f864fb0b3c559d6702f62 100644 (file)
@@ -555,7 +555,7 @@ export abstract class AbstractPool<
     }
     for (const [workerNodeKey, workerNode] of this.workerNodes.entries()) {
       workerNode.resetUsage()
-      this.sendWorkerStatisticsMessageToWorker(workerNodeKey)
+      this.sendStatisticsMessageToWorker(workerNodeKey)
     }
   }
 
@@ -698,7 +698,7 @@ export abstract class AbstractPool<
     workerNodeKey: number,
     workerId: number
   ): Promise<void> {
-    const waitForKillResponse = new Promise<void>((resolve, reject) => {
+    await new Promise<void>((resolve, reject) => {
       this.registerWorkerMessageListener(workerNodeKey, (message) => {
         if (message.kill === 'success') {
           resolve()
@@ -706,9 +706,8 @@ export abstract class AbstractPool<
           reject(new Error(`Worker ${workerId} kill message handling failed`))
         }
       })
+      this.sendToWorker(workerNodeKey, { kill: true, workerId })
     })
-    this.sendToWorker(workerNodeKey, { kill: true, workerId })
-    await waitForKillResponse
   }
 
   /**
@@ -999,8 +998,8 @@ export abstract class AbstractPool<
     this.registerWorkerMessageListener(workerNodeKey, this.workerListener())
     // Send the startup message to worker.
     this.sendStartupMessageToWorker(workerNodeKey)
-    // Send the worker statistics message to worker.
-    this.sendWorkerStatisticsMessageToWorker(workerNodeKey)
+    // Send the statistics message to worker.
+    this.sendStatisticsMessageToWorker(workerNodeKey)
   }
 
   /**
@@ -1011,11 +1010,11 @@ export abstract class AbstractPool<
   protected abstract sendStartupMessageToWorker (workerNodeKey: number): void
 
   /**
-   * Sends the worker statistics message to worker given its worker node key.
+   * Sends the statistics message to worker given its worker node key.
    *
    * @param workerNodeKey - The worker node key.
    */
-  private sendWorkerStatisticsMessageToWorker (workerNodeKey: number): void {
+  private sendStatisticsMessageToWorker (workerNodeKey: number): void {
     this.sendToWorker(workerNodeKey, {
       statistics: {
         runTime: