首页 > 其他分享 >[RxJS] mergeAll - mergeMap

[RxJS] mergeAll - mergeMap

时间:2022-10-18 21:24:51浏览次数:58  
标签:term const mergeMap return subscribe mergeAll RxJS input event

const input$ = fromEvent(textInput, 'keyup');

input$.pipe(
  map(event => {
    const term = event.target.value;
    return ajax.getJSON(`https://api.github.com/users/${term}`) // return a nested observable
  }),
  debounceTime(200),  
).subscribe((obs) => {
  obs.subscribe(console.log) // you have to subscribe again
})

 

One way can solve the problem is mergeAll: it will help to subscribe nested observable

const input$ = fromEvent(textInput, 'keyup');

input$.pipe(
  map(event => {
    const term = event.target.value;
    return ajax.getJSON(`https://api.github.com/users/${term}`) // return a nested observable
  }),
  debounceTime(200),
  mergeAll(),
).subscribe(console.log)

 

It is so common map + mergeAll, so can use mergeMap():

const input$ = fromEvent(textInput, 'keyup');

input$.pipe(
  debounceTime(200),  
  mergeMap(event => {
    const term = event.target.value;
    return ajax.getJSON(`https://api.github.com/users/${term}`) // return a nested observable
  }),
).subscribe(console.log)

 

标签:term,const,mergeMap,return,subscribe,mergeAll,RxJS,input,event
From: https://www.cnblogs.com/Answer1215/p/16804233.html

相关文章