Reputation: 14226
If I have a function like _.transform
.
Somewhere within the iteratee
function I encounter an error; how do I exit from the _.transform
function?
i.e.
{
try {
fs.readFileSync('dog_pics');
} catch(e) {
return;
}
}
What about _.map
? Which expects return
statements.
Upvotes: 2
Views: 6067
Reputation: 4050
_.transform
callback can return false
in order to stop iterating.
From lodash examples:
_.transform([2, 3, 4], function(result, n) {
result.push(n *= n);
return n % 2 == 0;
});
// → [4, 9]
As you can see, iteration breaks on third step, when n === 3
_.map
and _.reduce
doesn't support iteration stopping
Upvotes: 13
Reputation: 4363
Since _.transform builds a new return object, returning without setting a pushing onto the result would allow you to "jump" out of that iteration.
(I haven't actually tested this code.)
Upvotes: 0