Lodash offers a method called _.isError. Why do we need to use that method instead of val instanceof Error
?
If you look at the source, you'll see:
function isError(value) {
if (!isObjectLike(value)) {
return false;
}
var tag = baseGetTag(value);
return tag == errorTag || tag == domExcTag ||
(typeof value.message == 'string' && typeof value.name == 'string' && !isPlainObject(value));
}
I am not sure what cases this complexity handles that val instanceof Error
does not.
This is a bit contrived, but here is where _.isError(val)
and val instanceof Error
diverge:
function Foo() {
this.message="a";
this.name="a";
}
console.log( _.isError(new Foo) ) // true
console.log( new Foo instanceof Error ) // false
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
I cannot say if this is intended behavior on their part, but this is where the lodash method handles extra complexity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With