My application receives input, concatenates it to a string, and then requests JSON data. The response includes the following first two lines: https://i.sstatic.net/h6YNH.png
Now, I need to update my code to be asynchronous. It should make the initial call, wait for a response, retrieve the "id", store it in a variable, and use it in a second call. However, my current code is not functioning as expected. It's failing to retrieve the id because it is not truly asynchronous.
grabRAW(email){
this.grabID(email).subscribe(data => {
this.idList = data.data;
});
let id = this.idList[0].id;
return this.http.get(((this.url1.concat(id))).concat(this.url2))
.map((res: Response) => res.json())
.do((res:Response) => console.log(res))
}
grabID(email){
return this.http.get(this.url.concat(email))
.map((res: Response) => res.json())
.do((res:Response) => console.log(res))
}
The 'url' is concatenated with the email provided as input in the first call, and the id obtained from the first call should be concatenated between url1 and url2 for the second call. The goal is to consolidate everything into one function and ensure that the response of the second call is mapped and returned eventually.
EDIT: Here is the TypeScript function on the page that calls this service provider:
grabInfo(){
loader.present();
this.ServiceProvider.grabRAW(this.email).subscribe(data => {
this.infoList = data.data;
if (typeof this.infoList[0] != 'undefined'){
loader.dismiss();
this.navCtrl.push(ResultPage,{infoList: this.infoList})}
else{
loader.dismiss();
let alert = this.toastCtrl.create({
message: "No Data found for this Email Address",
duration: 4000,
position: 'bottom'
});
alert.present();
}
}
);
}