P
P
Pantene7422019-04-10 21:04:01
JavaScript
Pantene742, 2019-04-10 21:04:01

How to return data or make two more requests in Rx.js depending on the result of the first request?

I need to make a request via HttpClient (Angular) After the response comes, I need to return the data if the email does not exist in the database, and if it does, then transfer the stream to two more requests and return the already processed result of these two requests to the subscript callback (Observables since httpClient I use).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Demian Smith, 2019-04-10
@Pantene742

This is done via switchMap:

this.httpClient.get('/check-email').pipe(
  switchMap(emailResult => {
    if (emailResult !== exists) {
      return of(dataForNonExistingEmail);
    }
    
    return forkJoin(
      this.httpClient.get('/user-data1'),
      this.httpClient.get('/user-data2')
    );
  })
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question