递延对象问题

I have a problem i´m trying to solve by using deferred objects in javascript (and i am new to deferred objects).

The problem: The user tries to run a function (can be alot of different functions). If the function fails...it will try to login again and then try again (one time). If login fails. Well then all fails.

These functions and the login function contains an Ajax call that will be returned.

My Question is: Can i rely on that var dfd (at the end of the tryAjax function) to be executed last after all other code in the function has run?

Here is the code:

function tryAjax(func)
{
    var dfd = new jQuery.Deferred();
    window[func]().then(
    function(p1,p2,p3)
    {
        //Everything worked great. No need to login.
        dfd.resolve(p1,p2,p3);
    },
    function()
    {
        //func failed
        //try to login user again before trying.
        loginUser().then(
        function()
        {
            //Login success
            //Try to run func again.
            window[func]().then(
            function(p1,p2,p3)
            {
                //Func succes after login
                dfd.resolve(p1,p2,p3);
            },
            function(p1,p2,p3)
            {
                //Func failed after login
                dfd.reject(p1,p2,p3);
            });
        },
        function(p1,p2,p3)
        {
            //Login failed
            dfd.reject(p1,p2,p3);
        });
    });

    return dfd;   
}

And to call it:
tryAjax('getData').then(
function(p1,p2,p3)
{
    //Success  
},
function(p1,p2,p3)
{
    //Error
});

So basically, you want to execute 3 deferred in sequence, and stop after first success. I would go with a generic function like this one:

function dfdSequence() {
  var deferred = jQuery.Deferred();
  var execute = function(functions) {
    var promise = functions[0].apply(functions[0], args);
    promise.done(function() {
      deferred.resolve();
    });
    if (functions.length === 1) {
      // It was the last call
      promise.fail(function() {
        deferred.reject();
      });
    } else {
      // Fail, let's move on the next function
      promise.fail(function() {
        execute(functions.slice(1, functions.length));
      });
    }
  };
  execute(Array.prototype.slice.call(arguments, 0, arguments.length));
  return deferred.promise();
}

This function takes an arbitrary number of functions and execute them in sequence. Basically, each function returns a promise, and the next function is executed if the promise is rejected. Usage sample :

dfdSequence(function() {
  // Your first login attempt
  return $.ajax(...);
}, function() {
  // Your second login attempt
  return $.ajax(...);
}, function() {
  // Your last login attempt
  return $.ajax(...);
}).then(function() { /* Success! */ }, function() { /* Failure */ });

EDIT: Well, I guess i misunderstand the question. What about this piece of code:

function getData() {
  return $.ajax(...);
}
function loginUser() {
  return $.ajax(...);
}
var deferred = jQuery.Deferred();
getData().then(function() {
  // Success
  deferred.resolve();
}, function() {
  loginUser().then(function() {
    getData().done(function() {
      deferred.resolve();
    });
  }, function() {
    deferred.reject();
  });
}