[RxJS] Completing a Stream with TakeWhile

时间:2022-01-30 15:19:13

Subscribe can take three params:

subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);

If we want to stop the progame at some condition, so we need to notify complete function, which is the third param in subscribe.

Observable.combineLatest(
timer$,
input$,
(timer, input)=> ({count: timer.count, text: input})
)
.takeWhile((data)=> data.count <= )
.filter((data)=> data.count === parseInt(data.text))
.subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);

We can use takeWhile() function to end the program, so after the count = 3, "complete" will logout