load scripts in order example - part II wip

by Gerald Gillespie

HTML

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

JavaScript

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

var weirdfn = function() {
    var scripts = ["/echo/js/?delay=1&js=" + fcode('A1'), "/echo/js/?delay=1&js=" + fcode('A2'), "/echo/js/?delay=1&js=" + fcode('A3')];
    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);
            }); // end getscript
        }); // end pipe
    }); // end each : build the pipes from the array of scripts
   
    return deferred;// return promise

};

//$.getScript("/echo/js/?delay=3&js=" + fcode('B1'))
weirdfn().resolve(); 
// option 1: 

var anotherfn = $.when($.getScript("/echo/js/?delay=1&js="+fcode('B1')),
                       weirdfn()).then(function(){ anotherfn.state();});

console.log(anotherfn.state());
//