title: Alternative to throwing/catching errors with async code?
body: <p>I'm used to throwing an instance of some error class and having them be caught somewhere down the line in the app, to account for user error.</p>
  
  <p>An example might be validating the username:</p>
  
  <pre><code>function validateUsername (username) {
      if (!/^[a-z0-9_-]{3,15}$/.test(username)) {
          throw new ValidationError('Please enter 3-15 letters, digits, -, and/or _.');
      }
  }
  
  $('#username').blur(function () {
      try {
          validateUsername($(this).val());
      } catch (x) {
          $('&lt;p&gt;&lt;/p&gt;').addClass('error').text(x).insertAfter(this);
      }
  });
  </code></pre>
  
  <p>But now I'm realizing that I can't use these same practices for asynchronous calls. For example:</p>
  
  <pre><code>function checkUsernameAvailability (username) {
      $.get('/users/' + username).done(function () {
          // Done = user returned; therefore, username is unavailable
          // But I can't catch this error without resorting to something gross
          // like window.onerror
          throw new ValidationError('The username you entered is already in use.');
      });
  }
  </code></pre>
  
  <p>I could make <code>checkUsernameAvailability</code> accept a callback and/or return a promise and have it execute said callback with the availability of the username.</p>
  
  <pre><code>$('#username').blur(function () {
      checkUsernameAvailability(username, function (available) {
          !available &amp;&amp; alert('The username you entered is already in use.');
      });
  });
  </code></pre>
  
  <p>But part of what makes exceptions so powerful is that they can bubble up the stack until they get caught, whereas if I had another function that called another function that called <code>checkUsernameAvailability</code>, I'd need to pass the result of this callback manually all the way until I get to the place where I want to handle it.</p>
  
  <p>What are some of the alternative methods for passing errors up the stack? I can think of some of these, but none of them are as clean as native exceptions:</p>
  
  <ul>
  <li>Passing a flag, or the ValidationError instance, to a callback (Node.js approach could work too, passing an error or null as the first argument, and the data as the second); but then, if I don't want to handle it at that point in the stack, I need to pass the error up manually</li>
  <li>Or passing 2 callbacks to the <code>checkUsernameAvailability</code> function, a success callback and an error callback; this seems to have the same drawbacks as the previous point</li>
  <li>Triggering a "ValidationError" event so I can listen anywhere, but make sure to <code>return false;</code> in the handler so it doesn't execute higher in the stack; however, this pollutes the event namespace and could make it unclear as to which event listener will be executed first; plus, it's difficult to trace an event to its origin using the console</li>
  </ul>