Search code examples
javascriptarrayslodash

Lodash how to short circuit terminate


Is there a way to have Lodash terminate early such that this returns true instead of throwing exception?

_.chain([i => 'a', 0])
.reduce((prev, curr) => prev.concat(curr()), [])
.some(i => i == 'a')
.value()

Solution

  • I believe actually I'm thinking about the problem incorrectly, if the answer I'm wanting is the boolean result of .some(), then that should be my highest order query.

    _.chain([i => 'a', 0])
    .some(i => i() == 'a')
    .value();
    

    Then it evaluates the first item first and quits early. Expecting a Lodash specific answer is non-useful.