做多因AJAX同步调用preFER方式 [英] Prefer way of doing multiple dependent ajax synchronous call

查看:134
本文介绍了做多因AJAX同步调用preFER方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了做多依赖AJAX同步调用不同的方法。其中两个是被广泛采用的是成功了jQuery延迟的方法和回调。

我的问题是:
1)什么是使用一个在另一个的优势在哪里?
2)在什么情况下是一个pferred在另一个$ P $?
3)是否有比这两个任何其他更好的方法?

// jQuery的延迟方法 VAR延迟= $。当(     $获得('ajax_call_1'); ); defer.done(函数(){     $获得('ajax_call_2'); }); //回调成​​功 $(函数(){     $阿贾克斯({       网址:'/ ajax_call_1,       数据: { },       成功:功能(数据){         $获得('ajax_call_2');       }     });   } });

解决方案

使用承诺过回调的一些原因:

  1. 要简单得多序列的多个异步操作。
  2. 更易于建立涉及多个异步操作的条件逻辑。
  3. 要简单得多做可靠的错误处理涉及多个异步操作。
  4. 更易于构建可重用的异步接口。
  5. 在许多简单的与其它异步接口连接。
  6. 更易于应对可能发生深异步code,否则将导致沉默的失败是例外

在你的问题,进行排序jQuery的Ajax调用和捕获所有可能的错误,最简单的方法是使用从 $返回自然的承诺阿贾克斯()并把它们连:

  $。获得(ajax_call_1'),然后(函数(值){
    返回$获得('ajax_call_2');
}),然后(函数(结果){
    //用都在这里取得成功
},功能(错误){
    //在这里他们中的一个错误
});
 

或者,没有错误处理就像在你的例子:

  $。获得(ajax_call_1'),然后(函数(值){
    $获得('ajax_call_2');
})
 

没有理由使用这个结构在​​这里:

  // jQuery的延迟方法
VAR延迟= $。当(
    $获得('ajax_call_1');
);
 

由于 $。获得()已返回一个承诺,因此没有必要使用 $。当()只创建另一个承诺。 $。()时,当你有一个以上的承诺,你要知道,当所有的人都做了是有用的。其一诺言,你只需直接使用它 - 。没有理由使用 $时,()用一个诺言


您可以做你的第二个方式:

  //回调成​​功
    $阿贾克斯({
      网址:'/ ajax_call_1,
      数据: {  },
      成功:功能(数据){
        $获得('ajax_call_2');
      }
    });
 

由于这是嵌套回调编码只是非承诺的方式。主要的缺点是错误和测序多个操作的传播就会变得混乱而难以在不使用时的承诺。在如此简单的例子,试图从任阿贾克斯得到错误调用返回给调用者。这需要很多额外的code做到这一点。上面我的承诺,例如传播的所有错误返回给调用者给你。


至于你的具体问题:

  

1)什么是使用一个在另一个的优势在哪里?

你基本上问为什么用承诺在嵌套的回调。有数以百计的使用承诺的好处的文章。我会看看我能找到一两个,而是一个谷歌搜索为什么承诺与回调应该让你开始在一些阅读。

什么了不起关于JavaScript的承诺?

住的健全与异步编程

为什么我切换到承诺

  

2)在什么情况下是一个pferred在另一个$ P $?

据我所知,没有理由平原嵌套的回调是pferred超过使用承诺$ P $。一旦你有一个学习的承诺,你会pretty的多少总能找到他们一个更好的方式来code。我不会用承诺的唯一原因是,如果我试图让code这是与旧的浏览器兼容是没有的承诺,即使是这样,我可能只是包括polyfill使承诺得到了支持。

  

3)是否有比这两个任何其他更好的方法?

是的,看到我的第一个code的例子。


P.S。请注意,我选择只使用。那么() jQuery的承诺,因为这是ES6承诺的标准,这将使其在未来更容易,当jQuery的过渡的承诺是多个标准兼容的(它们工作)。当与承诺的其他来源确实使用了标准的接口你的code也将更加一致。

I have seen different ways of doing multiple dependent ajax synchronous call. Two of which is being widely adopted is the jquery defer method and callback on success.

My question is:
1) What is the advantage of using one over another?
2) In what situation is one is preferred over another?
3) Is there any other better method than these 2?

// jquery defer method
var defer = $.when(
    $.get('ajax_call_1');
);

defer.done(function () {
    $.get('ajax_call_2');
});



// callback on success
$(function(){ 
    $.ajax({
      url:'/ajax_call_1',
      data: {  },
      success: function(data){      
        $.get('ajax_call_2');
      }
    });
  }
});

解决方案

Some reasons to use Promises over Callbacks:

  1. Much simpler to sequence multiple asynchronous operations.
  2. Much simpler to build conditional logic involving multiple asynchronous operations.
  3. Much simpler to do robust error handling involving multiple asynchronous operations.
  4. Much simpler to build reusable asynchronous interfaces.
  5. Much simpler to interface with other asynchronous interfaces.
  6. Much simpler to deal with exceptions that might occur deep in asynchronous code that would otherwise cause silent failure

In your question, the simplest way to sequence jQuery Ajax calls and catch all possible errors is to use the natural promises returned from $.ajax() and chain them:

$.get('ajax_call_1').then(function(value) {
    return $.get('ajax_call_2');
}).then(function(result) {
    // success with both here
}, function(err) {
    // error with one of them here
});

Or, with no error handling like in your example:

$.get('ajax_call_1').then(function(value) {
    $.get('ajax_call_2');
})

There is no reason to use this construct here:

// jquery defer method
var defer = $.when(
    $.get('ajax_call_1');
);

because $.get() already returns a promise so there is no need to use $.when() to just create yet another promise. $.when() is useful when you have more than one promise and you want to know when all of them are done. For one promise, you just use it directly - no reason to use $.when() with a single promise.


You can do it your second way:

// callback on success
    $.ajax({
      url:'/ajax_call_1',
      data: {  },
      success: function(data){      
        $.get('ajax_call_2');
      }
    });

As this is just the non-promise way of coding with nested callbacks. The major disadvantage is that propagation of errors and sequencing multiple operations gets messy and difficult when not using promises. In just this simple example, try to get the error from either of the ajax calls back to the caller. It takes a lot of extra code to do that. My promise example above propagates all errors back to the caller for you.


As for your specific questions:

1) What is the advantage of using one over another?

You're basically asking why use promises over nested callbacks. There are hundreds of articles on the advantages of using promises. I will see if I can find one or two, but a Google search for "why promises vs. callbacks" should get you started on some reading.

What’s so great about JavaScript Promises?

Staying Sane with Asynchronous Programming

Why Am I Switching to Promises

2) In what situation is one is preferred over another?

I know of no reason why plain nested callbacks is preferred over using promises. Once you have an learn promises, you will pretty much always find them a better way to code. The only reason I would not use promises is if I was trying to make code that was compatible with old browsers that did not have promises and even then, I'd probably just include a polyfill so that promises were supported.

3) Is there any other better method than these 2?

Yes, see my first code example.


P.S. Note that I choose to only use .then() with jQuery promises because that is the ES6 promise standard and it will make it easier in the future when jQuery transitions its promises to be more standards-compatible (which they are working on). Your code will also be more consistent when interfacing with other sources of promises that do use the standard.

这篇关于做多因AJAX同步调用preFER方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆