1

Hello guys I'm trying to use a data object in html file and i'm using the async pipe and a subject to emit id and get server response.
Here is my code:

     logDetails$: Observable<LogDetails>;
     getDetails$ = new Subject<string>();

     this.logDetails$ = this.getDetails$.pipe(
        map(id => ApiRoutes.fileLogDetailsApiRoute.replace(":id", id)),
        switchMap(apiRoute => this.http.get<LogDetails>(apiRoute))
    );

I use an async pipe in my view to use subscribe for result.

*ngIf="logDetails$ | async; let details"

Now i want this behaviour: I emit the getDetails$ with id from multiple locations.
Then i need that before server call a null value for result get emmited to the view and then the server response (LogDetails object) after some delay.

  1. send a default value for result
  2. delay
  3. send server response

Can i use operators to achieve this?

1 Answer 1

2

You can use startWith and delay.

this.logDetails$ = this.getDetails$.pipe(
  map(id => ApiRoutes.fileLogDetailsApiRoute.replace(":id", id)),
  switchMap(apiRoute => this.http.get<LogDetails>(apiRoute).pipe(
    delay(1000),
    startWith(null)
  )),
);
Sign up to request clarification or add additional context in comments.

3 Comments

@fridoo You made my day! A question: when i use the startWith on http.get pipe it's working as expected, but when i move it to the first level pipe the first value is not get emitted. why? but others get.
@ArminTorkashvand The observable you in return in switchMap gets subscribed to when ever getDetails$ emits. The outer observable gets subscribe to only once (I guess).
@fridoo That's true!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.