javascript - Lodash - can you continue chaining after variable declaration? -
this bit of code:
var foo = [1, 2, 3], bar = _.chain(foo) .map(number => number * 2); console.log(bar.value()); bar.tap(numbers => { numbers.push(10000); }); console.log(bar.value());
the 10000
won't added bar.value()
. if move tap chain during actual variable chain, works fine. i'm has context of tap
called, can explain? seems nice init chain , modify later. thanks!
bin demonstration: http://jsbin.com/kidomeqalo/edit?html,js,console
js bin on jsbin.com
just adding bar.tap();
doesn't change anything. need include in chain:
bar = bar.tap(numbers => { numbers.push(10000); }); console.log(bar.value());
or
console.log(bar.tap(numbers => { numbers.push(10000); }).value());
on top of that, should not use tap
executing side effects. rather use bar.concat(10000).value()
or that, makes clear creates new result in functional way instead of mutating - becomes confusing sequences evaluated lazily.
Comments
Post a Comment