From 800bd00f70290df6a61f2fa02db652602a17d126 Mon Sep 17 00:00:00 2001 From: Bastien Gatellier Date: Sat, 13 Apr 2024 23:08:33 +0200 Subject: [PATCH] refactor: improve readability --- assets/js/services/AnalysisService.js | 57 ++++++++++----------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/assets/js/services/AnalysisService.js b/assets/js/services/AnalysisService.js index 050dfb0c..2e6ceaf4 100644 --- a/assets/js/services/AnalysisService.js +++ b/assets/js/services/AnalysisService.js @@ -22,35 +22,25 @@ class AnalysisService { try { EcoIndexDialog.openPendingAnalysis(url); - ApiService.newAnalysisTaskByURL(url).then( - (taskId) => { - ApiService.fetchAnalysisTaskById(taskId).then( - (taskResult) => { - const ecoindex = taskResult.ecoindex_result; + const taskId = await ApiService.newAnalysisTaskByURL(url) - if (taskResult.status === "SUCCESS" && ecoindex.status === "SUCCESS") { - ResultCacheService.add(ecoindex.detail); - redirectToResults(taskId, resultUrlPrefix); - } + const taskResult = await ApiService.fetchAnalysisTaskById(taskId) + + const ecoindex = taskResult.ecoindex_result; - if (taskResult.status === "SUCCESS" && ecoindex.status === "FAILURE") { - const e = taskResult.ecoindex_result.error; - EcoIndexDialog.openErrorMessage(e.status_code, e); - } + if (taskResult.status === "SUCCESS" && ecoindex.status === "SUCCESS") { + ResultCacheService.add(ecoindex.detail); + redirectToResults(taskId, resultUrlPrefix); + } - if (taskResult.status === "FAILURE") { - EcoIndexDialog.openErrorMessage(599, taskResult.task_error); - } - }, - (e) => { - this.#handleError(e); - } - ); - }, - (e) => { - this.#handleError(e); - } - ); + if (taskResult.status === "SUCCESS" && ecoindex.status === "FAILURE") { + const e = taskResult.ecoindex_result.error; + EcoIndexDialog.openErrorMessage(e.status_code, e); + } + + if (taskResult.status === "FAILURE") { + EcoIndexDialog.openErrorMessage(599, taskResult.task_error); + } } catch (e) { this.#handleError(e); } @@ -79,17 +69,10 @@ class AnalysisService { // Otherwise fetch from api try { EcoIndexDialog.openAnalysisRetrieval(); - await ApiService.fetchAnalysisById(id).then( - (result) => { - apiResult = result; - ResultCacheService.add(result); - redirectToResults(result.id, resultPagePrefix); - EcoIndexDialog.close(); - }, - (e) => { - this.#handleError(e); - } - ); + apiResult = await ApiService.fetchAnalysisById(id) + ResultCacheService.add(result); + redirectToResults(result.id, resultPagePrefix); + EcoIndexDialog.close(); } catch (e) { this.#handleError(e); return null;