Eli White (2017-07-16T19:54:59.000Z)
github at eli-white.com (2017-07-16T19:59:47.390Z)
This is definitely something that can be polyfilled (with different levels of naivety) but requires modifying built-ins which is a no-no. Here is an example that is more valuable in production than just debugging. Many other languages that support `.tap` enable a value to be returned. If it is, that value is passed down the rest of the chain instead of the initial value (but the return value is optional). This is also functionally similar to `.tee` in many languages (and bash). https://en.wikipedia.org/wiki/Tee_(command) One of the other common use cases for `.tap` is to be able to chain methods that act on the entire array. For example, if `.reverse` wasn't part of `Array.prototype` and instead you had a user function `myReverse(arr) => arr'`, then if you wanted to convert `[1,2,3]` into `["6","4","2"]`, then you'd have to do the following. It is a bit of a contrived example since I'm avoiding just calling `myReverse` before or after the chain. Imagine a more complex example and longer chain. ``` const value = [1, 2, 3].map(String); myReverse(value).map(num => num * 2); ``` With `.tap`, it can be part of the chain: ``` const value = [1, 2, 3] .map(String) .tap(myReverse) .map(num => num * 2); ``` Obviously this could be done with reduce, but it would require `myReverse` to have a different signature. Just trying to provide some reasoning why `.tap` is for more than just debugging. :)