Here is a simplified version of the service code I am using:
Load(Channel: any) {
//
let URL = Globals.AppSiteRoot + Channel.URL
return this.LoadData(URL)
}
Load_Default() {
let URL = Globals.AppSiteRoot + "dir1/somedata.XML"
console.log("XYZService.Load_Default------.------>>URL", URL)
//
return this.LoadData(URL)
.then(data => {
{
// do some processing here
console.log("XYZService.Load_Default<<P------.------data", data)
console.log("XYZService.Load_Default<<P------.------This.Data", this.Data)
}
});
}
// More information on async/await and Angular can be found at:
// https://medium.com/@balramchavan/using-async-await-feature-in-angular-587dd56fdc77
// https://v5.angular.io/api
// https://v5.angular.io/guide/comparing-observables
LoadData(URL: string) {
return new Promise(resolve => {
this.HttpClient.get(URL)
.map(res => res)
.subscribe(
data => {
console.log("XYZService.LoadData<<P------.------http.get=>data", data)
this.Data = data
resolve(this.Data)
},
err => {
console.log("XYZService.LoadData<<P------.------http:ERR", err)
}
);
});
}
Within an XYZcomponent, I use the following method:
Show(Channel: any) {
console.log("XYZComponent.Show------.------>>Channel>>" + Channel.URL)
//
this.XYZService.Load(Channel)
.then(data => {
console.log("XYZComponent.Show------.------>>data", data)
this.NavController.parent.select(1);
});
}
You can streamline the component code by utilizing async/await as shown below:
async ShowV2(Channel: any) {
console.log("XYZComponent.Show------.------>>Channel>>" + Channel.URL)
//
const data = await this.XYZService.Load(Channel)
console.log("XYZComponent------.------>>data", data)
//
this.NavController.parent.select(1)
}
If you are confused about whether to use Promises, Observables, or ReplaySubject(1), rest assured that it's a common issue faced by many developers.
Regarding error handling, some recommend avoiding HttpClient.get.subscribe in favor of .toPromise(), but proper error handling is crucial and must not be overlooked.
Async/await syntax may seem cleaner, but keep in mind that Observables still have their own advantages, especially in Angular development.
For your code in .Load() .Load_Default() and the component .Show() methods, consider expecting Observables and implementing them as needed.
Lastly, it's essential to establish clear coding patterns for event emitters like ReplaySubject(1) to avoid confusion when revisiting the code later.
If you're feeling lost in the sea of asynchronous operations, don't worry - simplicity is key, and with time and practice, you'll achieve your goal efficiently.
Please feel free to share any insights or template code with error-handling strategies you find effective in production environments.
npm show ionic version
5.4.16
npm show cordova version
9.0.0
npm show angular version
1.7.9
show @angular/core version
9.0.6
ng --version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 9.0.4
Node: 10.16.0
OS: win32 x64
Angular: 5.0.3
... common, compiler, compiler-cli, core, forms, http
... platform-browser, platform-browser-dynamic
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.4 (cli-only)
@angular-devkit/build-optimizer 0.0.35
@angular-devkit/core 9.0.4 (cli-only)
@angular-devkit/schematics 9.0.4 (cli-only)
@schematics/angular 9.0.4 (cli-only)
@schematics/update 0.900.4 (cli-only)
rxjs 5.5.2
typescript 2.8.3
webpack 3.12.0