3 ms·
[Nitpick] Instead of this: ;(async function () { try { // All your code } catch (e) { console.log(e) } })() Just do
by hombre_fatal 7y ago
[Nitpick] Instead of this:
;(async function () {
try {
// All your code
} catch (e) {
console.log(e)
}
})()
Just do this:
;(async function () {
// All your code
})().catch(console.error)
Overuse of try/catch is probably the #1 mistake I see when people use async/await.
- throwanem 7y agoThe point of async/await is to enable asynchronous code to participate in the same patterns synchronous code does, instead of being coerced into a promise-style interface. Using try/catch is part of that, and it's good practice here. If you're not going to take advantage of it, why bother with async/await at all, especially when using it at top level requires a workaround anyway?
- mrweasel 7y agoEither way, I find the syntax of both to be extremely ugly. Arguably this may simply be me not getting JavaScript. Starting a line with ; is just plain weird and the last bit })() seems like German, where all the stuff that you technically not need to understand the sentence is at the end. Maybe someone can explain with JavaScript developers love anonymous functions so much. Why not just name your function and make the whole thing a bit more readable.
- akamaozu 7y agoNot all JavaScript devs. Love the language, hate the popularity of anonymous functions and non-descriptive variable names. My production code as a sample. http://designbymobi.us/wp-content/uploads/2020/02/rabbitmq-customer-rpc-api.jpg http://designbymobi.us/wp-content/uploads/2020/02/rabbitmq-c...
- mrweasel 7y agoYour code sort of have the same issue. Why not define a function: function someName(p1, p2) {} and then when you need it for something, like a handler, just say: handler: someName
- akamaozu 7y agoHoped to show that JavaScript can be easier to read than you described. Obviously wrong. My mistake.
- tln 7y agoI don't think you can assume console.error is bound. Also it's good to avoid the possibility of blindly passing through unintended extra arguments (not an issue with catch, but is with many methods such as forEach etc). catch(err => console.error(err))