title: Elegantly Handling Reject on `await`ed Javascript Promise
body: <p>A great pattern with ES2017 async/await is:</p>
  
  <pre><code>async function () {
    try {
      var result = await some_promised_value()
    } catch (err) {
      console.log(`This block would be processed in
        a reject() callback with promise patterns
        but this is far more intuitive`)
      return false // or something less obtuse
    }
    result = do_something_to_result(result)
    return result;
  }
  </code></pre>
  
  <p>Being able to handle errors like that is really nice. But say I want to asynchronously get a value I'd like to protect it from reassignment (eg: a database session), but I still want to use the async/await pattern (purely because I think it's far more intuitive).</p>
  
  <p>The following won't work because const is block scoped:</p>
  
  <pre><code>async function () {
    try {
      const result = await get_session()
    } catch (err) {
      console.log(`This block should catch any
        instantiation errors.`)
      return false
    }
    // Can't get at result here because it is block scoped.
  }
  </code></pre>
  
  <p>Of course you could execute the rest of the function within the <code>try</code> block, but then you risk errors getting caught that should be let fall through somewhere else. (For example I hit this challenge writing tests where errors like test failures need to fall back to the test suite but wanted to handle driver instantiation myself. Perhaps I'm falling into some kind of anti-pattern here by not letting those errors fall back to the test suite.)</p>
  
  <p>The simple answer obviously is don't use that pattern here. Use Promises and callbacks instead. Or use a <code>var</code> and avoid block scoped <code>const</code> and <code>let</code>. But I like the benefits of reassignment protection and block scope for other considerations.</p>
  
  <p>[question]: <a href="https://stackoverflow.com/questions/41349331/is-there-a-way-to-wrap-an-await-async-try-catch-block-to-every-function">Is there a way to wrap an await/async try/catch block to every function?</a> Seems to be one potential solution, but doesn't quite come up as readable as <code>try/catch</code>. The fact that <code>try/catch</code> doesn't break function scope and I can use <code>return</code> from within a <code>try/catch</code> block seems to me to be more in line with the spirit of <code>async/await</code> bringing a more procedural-looking logic to asynchronous code.</p>
  
  <p>Ideally you'd want to do something like <code>const x = await y() catch (err)</code> or <code>const x = await y() || fail</code> But I can't think of anything with a similar flow that's syntactically correct.</p>
  
  <p>UPDATE:
  As suggested by @jmar777 below another alternative is:</p>
  
  <pre><code>const x = await y().catch(() =&gt; {/*handle errors here*/})
  </code></pre>
  
  <p>Which is probably the closest I've found to those last two examples in practice. But it breaks the <code>return</code> scope that blocks downstream execution in the examples above. Which is a nice synchronous-like way of handling things.</p>
  
  <p>Each solution has its trade offs.</p>
  
  <p>I've manually hoisted the variables with <code>let</code> at the top of the function block as a working solution (as described in jmar777's answer) still an interesting question to see the various approaches, so I'll leave the question open for now.</p>