$.when it doesn't work.

$.when it doesn't work.

Hello:

I have the following code:

  1. function LoadTiposIva() {
  2.         if (TiposIva.length == 0) {
  3.             //ajax function for fetch data
  4.             $.ajax({
  5.                 type: "GET",
  6.                 url: '/FacturasENet/GetTiposIva',
  7.                 success: function (data) {
  8.                     TiposIva = data;
  9.                     console.log("1", TiposIva)
  10.                     
  11.                 }
  12.             })
  13.         }
  14.        
  15.     }

  16.     function resto() {
  17.         renderTipoIva($('#TipoIvaId'));
  18.         console.log("2", TiposIva);
  19.     }

  20.     $.when(
  21.         LoadTiposIva()
  22.     ).done(
  23.         resto()
  24.     )
and when executing it it shows console "2" first and then "1", when it had to be backwards

   
I thought that "done" does not start running until it ends "when".
What am I doing wrong?
Thanks.