I've got a pair of nested AJAX request that do not behave correctly.
The first gets a list of albums, and the second one gets executed for every album, giving me a list of tracks. 
$.ajax({
    url:getPHPSite(),
    method:"POST",  
    data:{
        query: "SELECT id_album FROM albums;"
    },
    success: function(response){
      var albums=JSON.parse(response);
      for(var i=0; i<albums.length;i++){
          var albumID=albums[i]['id_album'];
             getTracks(function(courses) {
              //do something
            }
            }, albumID);
        }  
    }, ....//error handler and everything else
The getTracks() function actually performs the second AJAX request and returns an array of tracks with a callback
function getTracks(callback, albumID){
    $.ajax({
        url:getPHPSite(),
        method:"POST",   
        data:{
            query: "SELECT idtracks FROM tracksWHERE id_album=" + albumID + ";"
        },
        success: function(response){
            array_tracks=JSON.parse(response);   
            callback(array_tracks);
        }, //...everything else
By putting breakpoints, I discovered that the first for loop is executed separately 
albumID=1
albumID=2
albumID=3
...end
and only then the getTracks() is executed. 
I want the getTracks() to be executed for every iteration of albums.
Why does it happen and how can I resolve this?
 
    