lodash / lodash.com

The Lodash website.

Home Page:https://lodash.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

_.thru() example: _.chain() is redundant there?

verheyenkoen opened this issue · comments

Is there any reason to have a chain step in the _.thru() example? I guess _(...) is already chaining the lot?

_('  abc  ')
 .chain()  // <-- this line here
 .trim()
 .thru(function(value) {
   return [value];
 })
 .value();
// => ['abc']

Source:

<div class="highlight js"><pre><div><span class="name">_</span>(<span class="string">&apos;&#xA0;&#xA0;abc&#xA0;&#xA0;&apos;</span>)</div><div>&#xA0;<span class="delimiter method">.</span><span class="name">chain</span>()</div><div>&#xA0;<span class="delimiter method">.</span><span class="name">trim</span>()</div><div>&#xA0;<span class="delimiter method">.</span><span class="name">thru</span>(<span class="type">function</span>(value)&#xA0;{</div><div>&#xA0;&#xA0;&#xA0;return&#xA0;[value];</div><div>&#xA0;})</div><div>&#xA0;<span class="delimiter">.</span><span class="support">value</span>();</div><div><span class="comment">//&#xA0;=&gt;&#xA0;[&apos;abc&apos;]</span></div></pre></div>

Hi @verheyenkoen!

Lodash has implicit chaining so it will terminate the chain for things like trim which return a primitive string unless you call .chain() which activates explicit chaining.

I see, thanks for explaining.