frixo
frixo

Reputation: 61

Observable with an http.get inside

I'm starting with angular.

I'm trying to create an Observable to load and put in cache data. I whant to subscribe this observable from the HomeComponent off my app, and when the observable ping it because it's finish, I will appear buttons

But, I have a problem I have a http.get to get the data from function : charger_data() Then if I succeed to have the data, I will transform it and put it in cache

I whant my main function : charger_data_et_cache() return an observable

I don't manage to do that, could you help me please !!! ?

Thanks

  public charger_data_et_cache(force:boolean) : Observable<boolean>{

    this.charger_data().subscribe(
      (value) => {
        console.log('Observable change ! => ' + value);
        charger_cache(value);
      },
      (error)=> {
        console.log('Observable error!');
      },
      () => {
        console.log('Observable complete!');
      }
    )
    
    
    return Observable.create( observer => {
        
    
    });

  
 }
 
 
 
 public charger_data():Observable<string>{
      return this.http.get('/assets/txt/mystere.txt',{responseType: 'text'}).pipe(
        map(data =>  {
          return data;
        }),catchError( error => {
          return throwError( 'error : ' + error )
        })
        );
  }
  
  
  public charger_cache(data:string){
  
    //do stuff
  }

Upvotes: 1

Views: 72

Answers (1)

frixo
frixo

Reputation: 61

I have found !

public charger_data_et_cache(force:boolean) : Observable<boolean>{

    if(force == false && MystereService.mystere_charge){
      return Observable.create((observer) => observer.complete());
    }
    return this.charger_data(force).pipe(map(data => {
        this.charger_cache(data);
        console.log('charger_cache ok');
        return true;
    }),catchError( error => {
        return throwError( 'error' )
      }));

  }

Upvotes: 0

Related Questions