Rudy F
Rudy F

Reputation: 13

What is the best way to handle a race condition?

I am using angular with NGRX/store and I am doing an api call through an action. Then I select the result of this API call in my store. I am using a facade so I am storing my selector inside observable. My code is:

this.facade.getDescription(); // API Call
this.productDescription$ = this.facade.productDescription$;

And then in my template file, I am subscribing to that observable using async pipe.

My object came back empty as the initialState was empty and I think that it was selecting the data faster than the response of the API call.

So to handle this I am adding a variable in my reducer called 'isLoading'. And I am putting this in my template with *ngIf="!isLoading"

Is there another way like using concatMap or something to handle this issue?

Upvotes: 0

Views: 654

Answers (1)

alex
alex

Reputation: 705

You can alternatively use the observable directly in the ngIf, something like.:

ngIf="!!(this.productDescription$ | async) as productDesc"

By the way, ou can then use productDesc in your template directly, no need to resubscribe to the observable.

Upvotes: 0

Related Questions