Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a new utility function,
tap
. This is a common utility found in ecosystems like Laravel or Ruby. Underscore has an implementation as well.The concept is fairly simple: you give a value as the first argument to
tap
, and a callback as the second argument. The callback takes the value as a parameter, and is executed just beforetap
returns the initial value.This is useful for avoiding temporary variables:
This is the first example that came to my mind, and as you can see, the trade-off
tap
offers to avoid declaring a variable is a little bit of readability. Sometimes, it's better not to use it, but most of the times, I find it super useful.