Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Last active August 29, 2015 13:57
Show Gist options
  • Save jakearchibald/1b12a0e5414a69d9350f to your computer and use it in GitHub Desktop.
Save jakearchibald/1b12a0e5414a69d9350f to your computer and use it in GitHub Desktop.
<link rel="preload" href="/a.js" type="application/javascript" class="script-preload">
<link rel="preload" href="/b.js" type="application/javascript" class="script-preload">
<link rel="preload" href="/c.js" type="application/javascript" class="script-preload">
<script>
Array.from(document.querySelectorAll('.script-preload')).reduce(function(chain, link) {
var err = false;
var oldOnerror;
function errorListener(msg) {
err = msg;
if (oldOnerror) {
return oldOnerror.apply(arguments);
}
}
return chain.then(function() {
return link.loaded();
}).then(function() {
oldOnerror = window.onerror;
window.onerror = errorListener;
var script = document.createElement('script');
script.src = link.href;
document.head.appendChild(script);
return script.loaded();
}).then(function() {
window.onerror = oldOnerror;
if (err) throw Error(err);
});
}, Promise.resolve());
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment