Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Displaying Initial Data with StartWith

    John LindquistJohn Lindquist

    You often need to render out data before you stream begins from a click or another user interaction. This lessons shows how to use startWith to set the initial output before you trigger your stream.

    rxjsRxJS
    ^5.0.0
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 You'll notice that once the page starts we have to click start for anything to come down in the console. You can imagine if we were rendering the timer somewhere in the dom, you probably want zero to show. You'd want this to fire one time to push a zero in there instead of waiting for that start to happen.

    00:21 What we can do is instead of initializing scan with that second argument, we'll cut that out. We'll drop it up here as a constant called data. Then we'll use the StartWith operator.

    00:36 Now when I hit save, you can see we start with an object that has a count of zero. Then, when I click start, we go one, two...I'll stop, and then start. Three, four.

    00:50 That's because StartWith is going to set the initial value of scan, the initial accumulator, and then go ahead and push through to subscribe. In any stream you can send that initial value through and have it hit the subscribe block without having to trigger any other way of starting it.

    Discuss

    Discuss