Hi  hhdr104,

Yeah, that is not how observables or promises work. If you want to assign 
the payload of an observable to a property inside a class, do it inside a 
subscribe. Remeber that subscribing n a service is more or less an 
antipattern. 
You will run into race conditions if you do it like you are doing it in 
your example code.

The correct way would be something like:

    @Injectable()
    export class DataService {
        constructor(private http: HttpClient) { }
    
        public products: Observable<Product[]> = of([]);
    
        loadProducts(): Observable<Product[]>  {
            debugger;
            this.products = this.http.get<Product[]>("/api/products").pipe(
shareReplay(1));
            return this.products;
        }
    }

And then handle the observable properly wherever you use it. Preferable 
with the async pipe.

Regards
Sander

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to