weixin_33724659 2012-09-11 08:22 采纳率: 0%
浏览 13

递延对象问题

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
});
  • 写回答

1条回答 默认 最新

  • weixin_33694172 2012-09-12 11:31
    关注

    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();
      });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题