I am dynamically loading code (functions) from a server and executing it as javascript code then storing it in an array and executing. All these snippets of code must be ex
Javascript is essentially single-threaded so you don't need a mutex. Your fetch could set up flags such that subsequent fetch calls could avoid making ajax calls e.g.:
var beingFetched = {};//map onflight -> callbacks
function fetch(foo){
if (foo in fooArray){
//Do Nothing
} else {
if (beingFetched.foo) { //note empty array is truthy
//register a callback
var callback = function(r){
//anything you need to do wit the return object r
//maybe even eval it.
};
//the callback would more likely be an argument to fetch itself
//or you could use a promise API instead so that you can at your will
//register multiple callbacks - for error, for success etc.
beingFetched.foo.push(callback);
} else {
beingFetched.foo = [];//truthy
//Fetch foo via Ajax and execute
$.ajax("getFoo/"+foo).done(function() {
_.each(beingFetched.foo, function(cb){
cb.apply(cb,arguments);
});
delete beingFetched.foo;
});
}
}
}