In Javascript (or for the most part, ECMAscript in general), which is the most appropriate way to write this?
try { ajax.abort() } catch(e) { console.error (e) }
or
try { ajax.abort(); } catch(e) { console.error (e); }
It seems like semi-colons are unneeded for this situation but at the same time I normally write this on 5 lines instead of one in which case I used semicolons for their standard programmatic purpose (eol).
Both work, and I'm sure both will validate, so which is semantically correct?
In Javascript (or for the most part, ECMAscript in general), which is the most appropriate way to write this?
try { ajax.abort() } catch(e) { console.error (e) }
or
try { ajax.abort(); } catch(e) { console.error (e); }
It seems like semi-colons are unneeded for this situation but at the same time I normally write this on 5 lines instead of one in which case I used semicolons for their standard programmatic purpose (eol).
Both work, and I'm sure both will validate, so which is semantically correct?
Share Improve this question edited Mar 26, 2019 at 5:25 Jonathan Leffler 754k145 gold badges947 silver badges1.3k bronze badges asked Sep 29, 2013 at 16:25 JacksonkrJacksonkr 32.2k42 gold badges189 silver badges288 bronze badges 2- 1 They're semantically identical. – Pointy Commented Sep 29, 2013 at 16:27
- Read the official specification. – ComFreek Commented Sep 29, 2013 at 16:27
3 Answers
Reset to default 9Here's an inline try-catch for expressions. You can pute a value in one go, but still catch exceptions and fall back to a default value when evaluation fails - similar in spirit to the ternary ?:
operator:
function trycatch(func, fail) {
try { return func() }
catch(e) { return fail }
}
The expression must be wrapped up in a function, so it gets executed inside trycatch()
not outside of it. Example use:
let value = trycatch(() => JSON.parse("parsing fails on this input"), null)
They're semantically identical, and they're both syntactically correct, because of Automatic Semicolon Insertion.
This part is opinion:
I remend always writing the necessary semicolons explicitly and never relying on ASI. But even if you do that, ASI can bite you. The classic example is:
function foo() {
return
"testing";
}
If you call foo
, the return value will be undefined
, not "testing"
, because ASI kicks in and adds a ;
after the return
. Ouch.
You could try the following function. I added JSDoc to document the usage and included an example.
/**
* Wraps a callback in a try-catch and returns a default value upon failure
* @param {Function} fn - callback wrapped in a try-catch block
* @param {Any|Function} defaultValue - value or callback
* @param {Boolean} [silent=true] - if false, log the error
* @return {Any} the result of either fn (success) or defaultValue (failure)
*/
const ifError = (fn, defaultValue, silent = true) => {
try {
return fn();
} catch (e) {
if (!silent) {
console.error(e);
}
}
return defaultValue instanceof Function
? defaultValue()
: defaultValue;
}
const throwError = () => { throw new Error('oops'); }
console.log(ifError(() => JSON.parse('{}', null))); // {}
console.log(ifError(() => JSON.parse('foo'), null)); // null
console.log(ifError(throwError, () => 'Hello!')); // Hello!
console.log(ifError(throwError, 1, false)); // (log Error) 1
.as-console-wrapper { top: 0; max-height: 100% !important; }