I need to get some scripts from the different social networking site and cache them. I want the function to be asynchronous, and non-blocking.

I have settled with a script, but I am not really sure if it is not overly complicated to do the job.

I did replace a normal for loop, and this method is much faster. I do not know why, but it just is.

// Cached scritps
var cachedScriptPromises = {};

// Location of the scripts
var Scripts : {
};

// When the button is clicked all the scripts
// on the website. They will be filled
// as soon as the script is loaded.
for( var script in Scripts ){
jQuery.cachedGetScript( Scripts[script] );
}
});

// Function that get and caches the scripts
jQuery.cachedGetScript = function( script ) {
if ( !cachedScriptPromises[ script ] ) {
cachedScriptPromises[ script ] = jQuery.Deferred(function( defer ) {
jQuery.getScript( script ).then( defer.resolve, defer.reject );
}).promise();
}
return cachedScriptPromises[ script ];
};


Eventually this function can be expanded by adding some before and after actions. The thing is that the files are loaded so quickly that I have removed these features, but I am planning to do this as soon as the project becomes bigger.

Does anyone have any suggestions on making this better? Maybe some adjustments to the code, or go a complete other way with this.