title: Mithril ajax wrapper, m.request, converts failures to success
body: <p>This may well be some newbie thing about JS and or Mithril that I am unaware of, but I have written a caching wrapper around Mithril's <code>m.request()</code> to track multiple pending requests with the intention of coalescing multiple identical requests (which concept fits very well with the backend RESTful API).</p>
  
  <p>My wrapper function is this:</p>
  
  <pre><code>function sendRequest(sdrcfg,rqscfg) {
      var key=JSON.stringify(rqscfg);
      var ret=sdrcfg.cache[key];
  
      if(!ret) {
          var cleanup=function(dta) {
              delete sdrcfg.cache[key];
              sdrcfg.removed(key);
              if(Object.keys(sdrcfg.cache).length==0) { sdrcfg.completed(key); }
              return dta;
              };
  
          ret=m.request(rqscfg).then(cleanup,cleanup);
  
          if(Object.keys(sdrcfg.cache).length==0) { sdrcfg.activated(key); }
          sdrcfg.cache[key]=ret;
          sdrcfg.added(key);
          }
      return ret;
      }
  </code></pre>
  
  <p>The problem I am having is that when the <code>.then(cleanup,cleanup)</code> clause is present, any errors from the host are received by the success function of all subsequent <code>then()</code> clauses chained to the request with the net effect of converting a failure response to a success response for the downstream code.</p>
  
  <p>If I simply comment out the then clause above, then the application code has the success or failure function called as expected.</p>
  
  <p>It seems like a really straight-forward thing to want to do, and completely in line with Leo's example, but I am stumped. JS and, in particular, promises are new to me, so I don't know exactly how to use them, or if this is expected -- I am just following examples and trying to build on them.</p>
  
  <p>Where am I going wrong with this?</p>