What is the cleanest way to both throw an error and return a value from a Javascript function?
Here's one proposed approach as a starting point:
(e, v) => {
setTimeout(() => { throw(e) }, 0);
return v;
}
Here's a runnable snippet to demonstrate further:
var val = ((e, v) => {
setTimeout(() => { throw(e) }, 0); // will throw in console
return v;
})('errMSG', 1)
alert(val); // 1
Return an object that contains the value you are interested in, and an error:
return {
value: "foo",
error: new Error("bar")
}
The receiver can then throw the error as necessary
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