Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save doublejosh/19b38cd3285d0a70729b to your computer and use it in GitHub Desktop.

Select an option

Save doublejosh/19b38cd3285d0a70729b to your computer and use it in GitHub Desktop.
Sequentially load files
var loadedPromise,
paths = [
'//code.jquery.com/jquery-1.11.3.min.js',
'//cdnjs.cloudflare.com/ajax/libs/jStorage/0.4.12/jstorage.min.js'
];
function simpleInclude(path) {
return new Promise(function (resolve, reject) {
var script = document.createElement('script');
script.src = path;
document.head.appendChild(script);
script.onload = function() {
resolve();
};
});
}
// Sequential loading.
loadedPromise = paths.reduce(function (previous, current) {
return previous.then(function () {
return simpleInclude(current);
});
// Initial promise to kick off the chain.
}, Promise.resolve(true));
@jtwalters

Copy link
Copy Markdown

@doublejosh noticed you have previous and current vars declared outside the function they're used in. That's unnecessary. Can just remove lines 2 & 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment