Use Buffers to Store Values Until a Condition is Met

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

Buffers give you chance to gather values together until your ready to work with them. This pattern can be used for calculations, string manipulations, and many other scenarios.

Consider a solution where splitter argument is a function instead of a value. How could you capture the condition in that function rather than the way it was implemented in this lesson

Congrats on finishing Section 03: Use the Callback and Closure Pattern to Build Advanced Async Behaviors!πŸŽ‰πŸŽ‰

πŸ‘‰ CLICK HERE to continue to the next section, Implement the Pattern in Common Async Scenarios. πŸ‘ˆ

John Lindquist: [0:00] Let's create a split operator. I'll select all of this, duplicate it, and then name this split, and it was passing a value to split on which we'll call the splitter. Then implement the behavior that we want. Again, it's just writing something in the body of this function that defines what we want.

[0:18] I'm going to create a buffer. If the value is the same as the splitter, then I want to push out the buffer. Otherwise, just push the value into the buffer. Then we'd also have to clear the buffer after we push it out.

[0:37] Let's go and import this into our operators. I'm going to get rid of modify, so put the modify here. Our project currently looks like this where it's doing one letter at a time. Now, when I import split, and I say splitOnSpace, and I hit save, you'll see we get an array with the characters of Hello. Our buffer pushed in HELLO and then it shut down. We missed out on getting John.

[1:08] This is a scenario where we want a custom done behavior. In my createOperator there, I'm going to just swap that out for curry so that if value = done, then I can do this same behavior here where I push out the buffer, clear it out, and then send along done.

[1:30] Now if I hit Save, you'll see I get an array of Hello and an array of John. Let's go ahead and join these together. In the logic, there would be I want to map. At this point, these are arrays. I'll say map this array to Array.join. I'll hit Save. Now we'll get Hello and John.

[1:55] Finally, I'm just going to delete modify here. We only use that as a generic teaching tool where something like an appendOperator would be more appropriate.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today