load scripts in order example

by Gerald Gillespie

HTML

<div id="foo">
    <ul id="wtf"></ul></div>

JavaScript

/*
var deferred = new $.Deferred(),
    pipe = deferred;
*/
// This code will be loaded as a separate file
var fcode = function(i){
return  code = //"var d=new Date();$('#wtf').append('<li>'+d+'</li>');";
    "var%20d%3Dnew%20Date()%3B%24('%23wtf').append('*%20"+i+"%3A%20%3Cli%3E'%2Bd%2B'%3C%2Fli%3E')%3B";
    
}
// Load and run a JS file
//$.getScript();

scripts = ["/echo/js/?delay=1&js=" + fcode(1), 
              "/echo/js/?delay=1&js=" + fcode(2),
           "/echo/js/?delay=1&js=" + fcode(3)];
// n length nested dependencies

var deferred = new $.Deferred(),
    pipe = deferred;


$.each(scripts , function(i, val) {
     pipe = pipe.pipe(function(result) {
         console.log('result '+i+': ',result); // "result" is what gets returned thru the pipe from previous call
         return $.getScript(val, function() {
             console.log('loaded '+ val);
         });
     });
    console.log('some other sync stuff during the async')
});

deferred.resolve();