[RxJS] Logging a Stream with do()

To help understand your stream, you’ll almost always want to log out some the intermediate values to see how it has progressed during its lifespan. This lesson teaches you how to use do to log values in the middle of the stream without having an impact on the rest of the stream.

Observable.combineLatest(
    timer$.do((x)=> console.log(x)),
    input$.do((x)=> console.log(x)),
    (timer, input)=> ({count: timer.count, text: input})
)
    .takeWhile((data)=> data.count <= 3)
    .filter((data)=> data.count === parseInt(data.text))
    .do(()=>{console.log("score!!")}) 
    .reduce((acc, curr)=> acc + 1, 0)
    .subscribe(
        (x)=> console.log(x),
        err=> console.log(err),
        ()=> console.log('complete')
    );

We put servel do() block in the code, it doesn't affect any logic, just simply loggout what we want to see, so it is good when we want to debug the stream.

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