浏览代码

♻️ prefer async/await

jmacura 4 年之前
父节点
当前提交
76b09db043
共有 1 个文件被更改,包括 52 次插入52 次删除
  1. 52 52
      src/adjuster/adjuster.service.ts

+ 52 - 52
src/adjuster/adjuster.service.ts

@@ -68,60 +68,60 @@ export class AdjusterService {
     }
   }
 
-  calculateIndex(): void {
+  async calculateIndex(): Promise<void> {
     this._raiInProcess = true;
-    this.httpClient
-      .post(this.serviceBaseUrl + 'eu/scores/', {
-        factors: this.factors.map((f) => {
-          return {
-            factor: f.name,
-            weight: f.weight,
-            datasets: f.datasets
-              .filter((ds) => ds.included)
-              .map((ds) => ds.name),
-          };
-        }),
-      })
-      .toPromise()
-      .then((attractivenessData: any[]) => {
-        // Spread the 'aggregate' value between 0 and 1
-        const min = attractivenessData.reduce((a, b) =>
-          a.aggregate < b.aggregate ? a : b
-        ).aggregate;
-        const max = attractivenessData.reduce((a, b) =>
-          a.aggregate > b.aggregate ? a : b
-        ).aggregate;
-        const coefficient = 1 / (max - min);
-        const constant = -min * coefficient;
-        attractivenessData.forEach((a) => {
-          a.aggregate *= coefficient;
-          a.aggregate += constant;
-        });
-        // Store relation between region and its data in a hash-table-like structure
-        // more memory consuming, but faster then find()
-        const codeRecordRelations = {};
-        attractivenessData.forEach((a) => {
-          codeRecordRelations[a.code.toUpperCase()] = a;
-        });
-        console.time('forEach-Index');
-        this.processIndex(codeRecordRelations);
-        console.timeEnd('forEach-Index');
-        this._raiInProcess = false;
-        this.adjusterEventService.loaded.next({
-          success: true,
-          type: 'index',
-        });
-      })
-      .catch((error) => {
-        console.warn(`Error obtaining data from ${this.serviceBaseUrl}.`);
-        console.log(error);
-        this._raiInProcess = false;
-        this.adjusterEventService.loaded.next({
-          success: true,
-          type: 'index',
-          err: error,
-        });
+    let attractivenessData: any;
+    try {
+      attractivenessData = await this.httpClient
+        .post(this.serviceBaseUrl + 'eu/scores/', {
+          factors: this.factors.map((f) => {
+            return {
+              factor: f.name,
+              weight: f.weight,
+              datasets: f.datasets
+                .filter((ds) => ds.included)
+                .map((ds) => ds.name),
+            };
+          }),
+        })
+        .toPromise();
+    } catch (error) {
+      console.warn(`Error obtaining data from ${this.serviceBaseUrl}.`);
+      console.log(error);
+      this._raiInProcess = false;
+      this.adjusterEventService.loaded.next({
+        success: true,
+        type: 'index',
+        err: error,
       });
+    }
+    // Spread the 'aggregate' value between 0 and 1
+    const min = attractivenessData.reduce((a, b) =>
+      a.aggregate < b.aggregate ? a : b
+    ).aggregate;
+    const max = attractivenessData.reduce((a, b) =>
+      a.aggregate > b.aggregate ? a : b
+    ).aggregate;
+    const coefficient = 1 / (max - min);
+    const constant = -min * coefficient;
+    attractivenessData.forEach((a) => {
+      a.aggregate *= coefficient;
+      a.aggregate += constant;
+    });
+    // Store relation between region and its data in a hash-table-like structure
+    // more memory consuming, but faster then find()
+    const codeRecordRelations = {};
+    attractivenessData.forEach((a) => {
+      codeRecordRelations[a.code.toUpperCase()] = a;
+    });
+    console.time('forEach-Index');
+    this.processIndex(codeRecordRelations);
+    console.timeEnd('forEach-Index');
+    this._raiInProcess = false;
+    this.adjusterEventService.loaded.next({
+      success: true,
+      type: 'index',
+    });
   }
 
   calculateClusters(): void {