diff --git a/src/app/network-call/network-call.component.html b/src/app/network-call/network-call.component.html index 96c36ef..bcf449f 100644 --- a/src/app/network-call/network-call.component.html +++ b/src/app/network-call/network-call.component.html @@ -6,15 +6,21 @@ [disabled]="isLoading$ | async"> Load the car + +

Loading...

- +

Car data

- +
\ No newline at end of file diff --git a/src/app/network-call/network-call.component.scss b/src/app/network-call/network-call.component.scss index 5ed7c06..9c6807a 100644 --- a/src/app/network-call/network-call.component.scss +++ b/src/app/network-call/network-call.component.scss @@ -1,4 +1,9 @@ .component { + .actions { + display: flex; + gap: 1rem; + } + .loading { display: flex; flex-direction: column; diff --git a/src/app/network-call/network-call.component.ts b/src/app/network-call/network-call.component.ts index ca7f1db..7c576df 100644 --- a/src/app/network-call/network-call.component.ts +++ b/src/app/network-call/network-call.component.ts @@ -28,7 +28,10 @@ export class NetworkCallComponent { loadCar(): void { this.loadThroughAPromise(); - // this.loadThroughAnObservable(); + } + + loadCarViaAnObservable(): void { + this.loadThroughAnObservable(); } private loadThroughAPromise(): void { diff --git a/src/app/promises-example/promises-example.component.html b/src/app/promises-example/promises-example.component.html index 491046a..a04c983 100644 --- a/src/app/promises-example/promises-example.component.html +++ b/src/app/promises-example/promises-example.component.html @@ -1,4 +1,29 @@

Les promesses

- +
+ + + +
+ +
+ Number of loadings: {{numberOfLoadings}} +
+ +
+

Loading...

+ +
+
+

Car data

+
+ +
+
\ No newline at end of file diff --git a/src/app/promises-example/promises-example.component.scss b/src/app/promises-example/promises-example.component.scss index e69de29..e81662f 100644 --- a/src/app/promises-example/promises-example.component.scss +++ b/src/app/promises-example/promises-example.component.scss @@ -0,0 +1,11 @@ +.component { + .actions { + display: flex; + gap: 1rem; + } + .loading { + display: flex; + flex-direction: column; + align-items: center; + } +} \ No newline at end of file diff --git a/src/app/promises-example/promises-example.component.ts b/src/app/promises-example/promises-example.component.ts index 3be38c2..da608ab 100644 --- a/src/app/promises-example/promises-example.component.ts +++ b/src/app/promises-example/promises-example.component.ts @@ -1,24 +1,95 @@ -import { Component } from '@angular/core'; +import {Component} from '@angular/core'; import {Car} from "../core/model/car"; +import {MatSnackBar} from "@angular/material/snack-bar"; -@Component({ - selector: 'app-promises-example', - templateUrl: './promises-example.component.html', - styleUrls: ['./promises-example.component.scss'] -}) -export class PromisesExampleComponent { - car?: Car = { +const CAR: Car = { id: 1, brand: 'Toyota', model: 'Yaris', power: 12, numberOfSeats: 5 - }; +}; - /* - // Async / Await -> + comparaison avec les then - // new Promise "à la main" - // les then chaînés - // catch (avec 1 ou plusieurs then) -> try/catch avec un await - */ +@Component({ + selector: 'app-promises-example', + templateUrl: './promises-example.component.html', + styleUrls: ['./promises-example.component.scss'] +}) +export class PromisesExampleComponent { + car?: Car; + isLoading: boolean = false; + isLoaded: boolean = false; + numberOfLoadings: number = 0; + + constructor( + private snackBar: MatSnackBar + ) { + } + + // ****************************************** + // Mode Promise avec les then/catch/finally + // ****************************************** + showCarViaAPromise(): void { + this.loadCarViaAPromise(); + } + + private loadCarViaAPromise(): void { + this.isLoading = true; + this.isLoaded = false; + + this.findViaPromise() + .then(car => { + this.car = car; + this.isLoaded = true; + }) + .catch(error => { + this.snackBar.open(`Error while loading car. Cause : ${error}`); + }) + .finally(() => { + this.isLoading = false; + this.numberOfLoadings++; + }); + } + + private findViaPromise(): Promise { + return new Promise((resolve, reject) => { + if (this.numberOfLoadings % 2 === 0) { + setTimeout(() => { + resolve(CAR); + }, 2000); + } else { + reject('It\'s an odd call number.'); + } + }); + } + + // ****************************************** + // Mode async/await + // ****************************************** + showCarViaAsyncAwaitProcess(): void { + this.loadCarViaAsyncAwaitProcess(); + } + + private async loadCarViaAsyncAwaitProcess(): Promise { + this.isLoading = true; + this.isLoaded = false; + + try { + this.car = await this.findViaAsyncAwaitProcess(); + this.isLoaded = true; + } catch (error) { + this.snackBar.open(`Error while loading car. Cause : ${error}`); + } + + this.numberOfLoadings++; + this.isLoading = false; + } + + private async findViaAsyncAwaitProcess(): Promise { + if (this.numberOfLoadings % 2 !== 0) { + throw 'It\'s an odd call number.'; + } + await new Promise(resolve => setTimeout(resolve, 2000)); + return CAR; + } } diff --git a/src/styles.scss b/src/styles.scss index 63ea5c4..c5f8a68 100644 --- a/src/styles.scss +++ b/src/styles.scss @@ -7,4 +7,8 @@ body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; } display: flex; flex-direction: row; justify-content: center; +} + +.section { + margin: 1rem 0; } \ No newline at end of file