0
arrayDaSpedire = ['345', '119', '567'];
for (var x=0; x < arrayDaSpedire.length; x++) {
    var ddt = arrayDaSpedire[x][0];
    var fnPagamento = getPagamentoRiparazione(ddt);
    $.when(fnPagamento).done(function(data) {
        console.log(ddt);
    });
}

Why this code returns always last string? Ok, becouse when ajax call terminates ddt = 567.. So how to log local ddt?

xfolder
  • 95
  • 1
  • 10
  • because, `ajax` is `asynchronous` and value of `ddt` keeps changing for the last one as `for-loop` will not wait `ajax` to complete.. – Guruprasad J Rao Jan 31 '17 at 10:28
  • The issue is because the loop finishes executing (hence `ddt = 567`) before any of the async requests complete. You can use a closure to solve this. See the duplicate for more info – Rory McCrossan Jan 31 '17 at 10:29
  • i just wrote this in the question.. ;) but how to log local ddt? – xfolder Jan 31 '17 at 10:38

0 Answers0