Match Function Requirements with Lodash Partial and Flip

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

Functions returning functions returning functions can begin to look a bit unwieldy. The arrow function has helped the syntax a lot, but maybe using a curry utility function to combine all the arguments into a single function will help your syntax look a little cleaner. Currying isn't necessary for our pattern, but you'll definitely see it used in many patterns involving functions returning functions and maybe you'll grow to love it.

Instructor: [0:00] It's also worth noting the value of a function called partial, which we can use to supply arguments one at a time and return functions that still need arguments. It comes close to being useful here.

[0:13] Let's say I comment out addListener. With window.addEventListener, which would typically look like this, I'll say copy, which tracks when I try and copy something from the page. Then you'd supply an eventListener.

[0:27] Instead, I could turn this into partial then wrap window eventListener. I want to apply copy and this will return a function that expects a listener, so by our definition, a broadcaster. I'll hit Save here and you'll see that cancel1 is not a function, meaning that this did not return a function that could cancel or remove the eventListener from the window.

[0:50] Let me refresh this. If I try and copy from the page, Command-C, you can see that it's firing a bunch of events. It's definitely good to be aware of, even though it doesn't quite meet our requirements of returning a function that can cancel this out.

[1:05] If I were to try and partial on a setInterval, so I'll say partial(setInterval), the signature of setInterval looks like setInterval, and then the callback, but in our situation, we need the callback to come last. We need to flip a callback and the amount of time which it would take.

[1:25] To do that, we could bring in another function, which is called flip. This can change the order of arguments. I'll say flip on the setInterval, and then pass in one second. Now we'll have two functions which are both really close. You'll see it's still ticking away, even though there's no way to stop it. This can still copy, even though there's no way to remove the event.

[1:48] As you're working with APIs, you don't have a lot of control over, and you want them to fit into your patterns, there are many helpful functions in Lodash that can help you manage applying individual arguments, changing the order of arguments, and other helpful utilities.

egghead
egghead
~ 30 minutes 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