[RxJS] Completing a Stream with TakeWhile

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 <= 3)
    .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

原文地址:https://www.cnblogs.com/Answer1215/p/5265967.html