Z
Z
zlodiak2018-03-03 23:30:32
Angular
zlodiak, 2018-03-03 23:30:32

How to merge two requests using mergeMap?

I would like to do the following: use an observable to track clicks in an area of ​​the page. And after each click, send a request to get a list of github users.

I can create two separate requests and send them in turn. But would like to merge these two queries using mergeMap. Here is my attempt:

constructor(private httpClient: HttpClient) { }

  ngOnInit() {
    Observable.fromEvent(document, 'click')
    .mergeMap(() => this.httpClient.get('https://api.github.com/users'))
      .subscribe((e) => {
        console.log(e);
        //console.log(e['pageX'], e['pageY']);
      });
  }


The problem is that console.log(e) only outputs a list of users, and the click event object doesn't output

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-03-03
@zlodiak

.mergeMap(() => this.httpClient.get('https://api.github.com/users'), (event, users) => ({ event, users }))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question