title: RequireJS: Local fallback for if CDN fails
body: <p>In my backbone app, I need to provide a fallback for each required file, in the case that the CDN that delivers them fails.</p>
  
  <p>I have tried overwriting <code>require.onError</code> like so:</p>
  
  <pre><code>require.onError = function (err) {
      if (err.requireType === 'timeout') {
          var url = err.requireModules;
  
          if (!!~url.indexOf("jquery/"))
              console.warn("CDN timed out, falling back to local jQuery.js")
              require(["libs/jquery"]);
              return;
          if (!!~url.indexOf("jqueryui/"))
              console.warn("CDN timed out, falling back to local jQueryUI.js")
              require(["libs/jqueryui"]);
              return;
          if (!!~url.indexOf("underscore"))
              console.warn("CDN timed out, falling back to local underscore.js")
              require(["libs/underscore"]);
              return;
          if (!!~url.indexOf("backbone"))
              console.warn("CDN timed out, falling back to local backbone.js")
              require(["libs/backbone"]);
              return;
      }
  }
  </code></pre>
  
  <p>The problem is that this will asynchronously load the fallback files. I need these files to load in order, just like the original require statement, where I use the <code>order!</code> plugin.</p>
  
  <p>With the overridden <code>onError</code>: when the CDN fails to load, the fallback load is started, but not waited for. This presents a problem because the scripts are ordered to be loaded based on their dependencies. Here is a look at my original <code>require</code> statement, that depends on the CDN:</p>
  
  <pre><code>require([
      "order!http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js",
      "order!http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js",
      "order!http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js",
      "order!http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js",
      "order!utils/date",
      "order!core/core",
      "order!core/errors",
      "order!core/constants"
  ], function() {
      ...
  }
  </code></pre>