From e845326e2a3e4d3bf9fbc599af2179926f8aca9a Mon Sep 17 00:00:00 2001 From: "mathias.chouet" <mathias.chouet@irstea.fr> Date: Fri, 4 Sep 2020 16:09:27 +0200 Subject: [PATCH] Convert remaining promises to async/await --- src/app/app.component.ts | 55 +++++++++---------- .../services/internationalisation.service.ts | 31 +++++------ 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 713ba435a..6ffb143b0 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -642,39 +642,38 @@ export class AppComponent implements OnInit, OnDestroy, Observer { }); } - public loadSessionFile(f: File, info?: any) { + public async loadSessionFile(f: File, info?: any) { // notes merge detection: was there already some notes ? const existingNotes = Session.getInstance().documentation; // load - this.formulaireService.loadSession(f, info) - .then((data) => { - if (data.hasErrors) { - this.notificationsService.notify(this.intlService.localizeText("ERROR_PROBLEM_LOADING_SESSION"), 3500); - } else { - if (data.loaded && data.loaded.length > 0) { - if (!isDevMode()) { - this.matomoTracker.trackEvent("userAction", "loadSession"); - } - // notes merge detection: was there already some notes ? - const currentNotes = Session.getInstance().documentation; - if (existingNotes !== "" && currentNotes !== existingNotes) { - this.notificationsService.notify(this.intlService.localizeText("WARNING_SESSION_LOAD_NOTES_MERGED"), 3500); - } - // go to calc or diagram depending on what was loaded - if (data.loaded.length > 1) { - this.toDiagram(); - } else { - this.toCalc(data.loaded[0]); - } + try { + const data = await this.formulaireService.loadSession(f, info); + if (data.hasErrors) { + this.notificationsService.notify(this.intlService.localizeText("ERROR_PROBLEM_LOADING_SESSION"), 3500); + } else { + if (data.loaded && data.loaded.length > 0) { + if (!isDevMode()) { + this.matomoTracker.trackEvent("userAction", "loadSession"); + } + // notes merge detection: was there already some notes ? + const currentNotes = Session.getInstance().documentation; + if (existingNotes !== "" && currentNotes !== existingNotes) { + this.notificationsService.notify(this.intlService.localizeText("WARNING_SESSION_LOAD_NOTES_MERGED"), 3500); + } + // go to calc or diagram depending on what was loaded + if (data.loaded.length > 1) { + this.toDiagram(); + } else { + this.toCalc(data.loaded[0]); } } - }) - .catch((err) => { - this.notificationsService.notify(this.intlService.localizeText("ERROR_LOADING_SESSION"), 3500); - console.error("error loading session - ", err); - // rollback to ensure session is clean - this.doEmptySession(); - }); + } + } catch (err) { + this.notificationsService.notify(this.intlService.localizeText("ERROR_LOADING_SESSION"), 3500); + console.error("error loading session - ", err); + // rollback to ensure session is clean + this.doEmptySession(); + } } /** diff --git a/src/app/services/internationalisation.service.ts b/src/app/services/internationalisation.service.ts index 683f84565..05af49767 100644 --- a/src/app/services/internationalisation.service.ts +++ b/src/app/services/internationalisation.service.ts @@ -79,44 +79,43 @@ export class I18nService extends Observable implements Observer { promisesList.push(this.loadLocalisation(calcType).catch((err) => { /* silent fail */ })); } } - await Promise.all(promisesList).then(() => { - this.httpGetMessages(code).then((res: any) => { - that._Messages = res; - // propagate language change to all application - that.notifyObservers(undefined); - }); - }); + await Promise.all(promisesList); + const res = await this.httpGetMessages(code); + that._Messages = res; + // propagate language change to all application + that.notifyObservers(undefined); } } /** * Loads the localisation file dedicated to calculator type ct; uses cache if available */ - public loadLocalisation(calc: CalculatorType): Promise<any> { + public async loadLocalisation(calc: CalculatorType): Promise<any> { const lang = this.currentLanguage; - return this.loadLocalisationForLang(calc, lang).then((localisation) => { - return localisation as StringMap; - }).catch((e) => { + try { + return await this.loadLocalisationForLang(calc, lang) as StringMap; + } catch (e) { return ""; - }); + }; } /** * Loads the localisation file dedicated to calculator type ct for language lang; * keeps it in cache for subsequent calls () */ - private loadLocalisationForLang(calc: CalculatorType, lang: string): Promise<any> { + private async loadLocalisationForLang(calc: CalculatorType, lang: string): Promise<any> { const ct = String(calc); // if not already in cache if (! Object.keys(this._languageCache).includes(ct) || ! Object.keys(this._languageCache[calc]).includes(lang)) { const f: string = FormulaireService.getConfigPathPrefix(calc) + lang + ".json"; - return this.httpService.httpGetRequestPromise(f).then((localisation) => { + try { + const localisation = await this.httpService.httpGetRequestPromise(f); this._languageCache[ct] = this._languageCache[ct] || {}; this._languageCache[ct][lang] = localisation; return localisation as StringMap; - }).catch((e) => { + } catch (e) { throw new Error(`LOCALISATION_FILE_NOT_FOUND "${f}"`); - }); + } } else { return new Promise((resolve, reject) => { resolve(); // does nothing but complies with Promise expectation -- GitLab