C
C
cocomuffin2019-04-11 13:43:51
Angular
cocomuffin, 2019-04-11 13:43:51

RXJS: how to correctly send multiple requests in parallel?

Good day!
The case is this: there is an http request to the server that returns the ID of a random hero.
As soon as I get the ID, I need to send two new requests in parallel to get the hero's house and his state.
As soon as I get a successful response from both requests, I want to display DONE in the console.
I can’t figure out how to send two requests instead of one as the second step (for example, mergeMap will send only one request)

const getCharacter$ = this.http.get('api/characters');

const getCharacterHome$ = (id) => this.http.get(`api/homes/${id}`);
const getCharacterState$ = (id) => this.http.get(`api/states/${id}`);

getCharacter$.pipe(
        map(result => result.id),
        //mergeMap, concatMap, forkJoin, ... ?
  mergeMap(id => // Тут надо отправить два запроса... )
).subscribe(ok => console.log('DONE'), error => console.log(error));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cocomuffin, 2019-04-11
@cocomuffin

Oh, it seems like this rolled:

getCharacter$.pipe(
        map(result => result.id),
        mergeMap(id => {
return forkJoin(getCharacterHome(id), getCharacterState(id))} )
).subscribe(ok => console.log('DONE'), error => console.log(error));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question