Skip to content

Commit 3a473cb

Browse files
committed
Async action with mobx
1 parent 23b3048 commit 3a473cb

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/app/about/about-store.service.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { Injectable } from '@angular/core';
2+
import { runInAction } from 'mobx';
23
import { action, observable } from 'mobx-angular';
34

5+
import { NasaService } from '../shared/nasa.service';
6+
47
@Injectable({
58
providedIn: 'root',
69
})
710
export class AboutStoreService {
811
@observable title: string;
912

10-
constructor() {
13+
constructor(private nasaService: NasaService) {
1114
this.initialize();
1215
}
1316

@@ -16,9 +19,11 @@ export class AboutStoreService {
1619
this.title = 'about:hello world';
1720
}
1821

19-
@action
20-
randomTitle() {
21-
// tslint:disable-next-line:no-magic-numbers
22-
this.title = Math.random().toString(32);
22+
async randomTitle() {
23+
const response = await this.nasaService.getApod();
24+
25+
runInAction(() => {
26+
this.title = response.title;
27+
});
2328
}
2429
}

src/app/about/about.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class AboutComponent {
3636
this.actions.getNasaApod();
3737
}
3838

39-
updateTitle() {
40-
this.store.about.randomTitle();
39+
async updateTitle() {
40+
await this.store.about.randomTitle();
4141
}
4242
}

0 commit comments

Comments
 (0)