title: JavaScript promise architecture: Squelch an error
body: <p>I've been trying to add better promise architecture to our codebase so that there's fewer hardcoded ties between functions, and promises have been useful for that. However, once in a while I have some difficulty figuring out the best way of designing a method.</p>
  
  <p>In this case, here's my stubbed-out functions (sort of pseudocode)</p>
  
  <pre><code>/* public function */
  function validateUserPreference() {
  return askUserPreference().then(saveUserPreference);
  }
  
  /* uses several ajax calls and modal dialogs to confirm the user's preference,
    returning true/false in a promise object. */
  function askUserPreference() ...
  
  /* Saves the user's preference to the server. Usually, try to call this
    before resolving validateUserPreference, as a convenience to those running it. */
  function saveUserPreference() ...
  </code></pre>
  
  <p>Now, here's the tricky bit; error resolution. Often when a series of promises fail, you just want to throw up a dialog to the user telling them something bad happened. But in this situation, I want to try saving the user's preference, BUT it's not a blocker if it doesn't manage to. I don't want to just add <code>then(saveUserPreference)</code> and return the original promise, because once <code>validateUserPreference()</code> returns, the page is going to submit straight away.</p>
  
  <p>Basically, I'd like to pass along the value from <code>askUserPreference</code> in my <code>then</code> function, and not resolve it until the other promise finishes, but then somehow ignore any <code>reject</code> failures that come up in <code>saveUserPreference</code>, and return that same value.</p>
  
  <p>I'm currently using Dojo's promise API, so I can make use of any libraries they have.</p>