JSON data on promise return, need advice

Hi All

i am using the following provider function below to call my api, which it does successfully.

I am calling this function from a provider on anther call.

The output if i am correct is a promise object with a my returned json data. When i look at object using a console.log. I see addtional element before my data e.g. __zone_symbol__value.

How do i get the json as i would need it?

gateway_list(){

           return new Promise(resolve => {
           
           this.http.get(this.Gateway_List_api,{
           headers: new HttpHeaders().set('ZUMO-API-VERSION','2.0.0'),                             
           params: new HttpParams().set('id', this.My_login_ID),
           })                                                                                                                    
           .subscribe(data => {
            resolve(data);                
            console.log(data);                                
            }, err => {
            console.log(err);
            });
            });

           }        
           
  }

HttpClient is already giving you a perfectly good Observable. Use it and completely forget about Promises here:

gatewayList(): Observable<string[]> {
  return this.http.get<string[]>(...);
}

Replace string[] with whatever business object interface you might prefer.

Never subscribe in the same place an Observable is created, so the subscribe call is going to be in whoever is calling gatewayList(), and in there is the data you want.

The answer to what appears to be your larger question of “how do I make asynchronous things work synchronously, because all this asynchronous stuff makes my head spin” is really “you can’t”, and there isn’t really any substitute for taking the time to slog through learning rx from places such as here.

1 Like

Ok, thanks. I am very knew to ionic and angular

This is what i got now. If i am correct Observable keep updating the variables .

gateway_list(): Observable{
return this.http.get(this.Gateway_List_api,{
headers: new HttpHeaders().set(‘ZUMO-API-VERSION’,‘2.0.0’),
params: new HttpParams().set(‘id’, this.My_login_ID),
})
}

It seems that its not output my api response, have i done something wrong?

Sorry, i got it working by call the function like

  this.azureservice.gateway_list()
      .subscribe(data => { 
        this.gatewaylist=data;                   
        console.log(data);                                
        }, err => {
        console.log(err);
      });

FYI, you can use .toPromise()

2 Likes