Skip to content

Instantly share code, notes, and snippets.

@ngryman
Last active February 1, 2018 10:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ngryman/7309432 to your computer and use it in GitHub Desktop.
Save ngryman/7309432 to your computer and use it in GitHub Desktop.
jQuery parallel script loading keeping execution order.
/**
* Load scripts in parallel keeping execution order.
* @param {array} An array of script urls. They will parsed in the order of the array.
* @returns {$.Deferred}
*/
function getScripts(scripts) {
var xhrs = scripts.map(function(url) {
return $.ajax({
url: url,
dataType: 'text',
cache: true
});
});
return $.when.apply($, xhrs).done(function() {
Array.prototype.forEach.call(arguments, function(res) {
eval.call(this, res[0]);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment