DEV Community

Discussion on: Hoist Your Vars! (Variable Hoisting in JavaScript)

Collapse
 
beardedhen profile image
Ryan Schumacher

A good example of when you might want to do this is if it’s a known exception and you are going to hand wave it away.

(async () => {
let result
try {
  // catch network issues
  const response = await fetch('https://example.com')
  // catch non-json data
  const data = await response.json()
  // catch inaccessible object
  result = data.messages
} catch(e) {
  result = { messages: [] }
}
})().then().catch()