简体   繁体   English

保证Javascript中的异步请求回调顺序

[英]Guaranteeing asynchronous request callback order in Javascript

In Javascript, I have two asychronous requests for data: 在Javascript中,我有两个对数据的异步请求:

$.getJSON('http://foo.com', fooQuery, fooSuccess(data));
$.getJSON('http://bar.com', barQuery, barSuccess(data));

and two callbacks to process the received data: 和两个回调来处理收到的数据:

fooSuccess(data) { // Stuff }
barSuccess(data) { // More Stuff }

How do I ensure barSuccess is executed only after fooSuccess completes? 如何确保仅在fooSuccess完成后执行barSuccess?

Notes: 笔记:

  • I want to keep the data requests as they are: asynchronous and non-blocking (since server responses may take a while). 我希望保持数据请求的原样:异步和非阻塞(因为服务器响应可能需要一段时间)。
  • But, I want the callbacks that process the data to be executed sequentially. 但是,我希望处理数据的回调顺序执行。 That is, I do not want to execute barSuccess until fooSuccess completes. 也就是说,在fooSuccess完成之前,我不想执行barSuccess。

Thanks so much for your wisdom and help! 非常感谢你的智慧和帮助!

Here is how you would do it using the jQuery deferred object that is returned by ajax requests. 以下是使用ajax请求返回的jQuery延迟对象的方法。

var fooDfd = $.getJSON('http://foo.com', fooQuery);
var barDfd = $.getJSON('http://bar.com', barQuery);

fooDfd.then(function(fooData){
    fooSuccess(fooData);
    barDfd.then(barSuccess);
});

The best way would be to utilize the jQuery when().done() functionality like this: 最好的方法是使用jQuery when().done()功能:

$.when(
    $.getJSON('http://foo.com', fooQuery, fooSuccess(data)), 
    $.getJSON('http://bar.com', barQuery, barSuccess(data))
).done(function(arg1, arg2){
    fooSuccess(arg1);
    barSuccess(arg2);
});

This allow simultaneous execution of the AJAX requests and guaranteed execution of the done() function once all requests has successfully completed. 所有请求成功完成后,这允许同时执行AJAX请求并保证执行done()函数。

I'm follow this very interesting post from a half an hour ago, when appear the elegant solution presented by @Mike Brant I quickly was to dive in the jquery library to see how the magic is made. 半小时前我正在关注这篇非常有趣的帖子,当看到由@Mike Brant呈现的优雅解决方案时,我很快就会潜入jquery库,看看魔术是如何制作的。 Don't you? 不是吗? I recommend, is very interesting! 我推荐,非常有趣! BTW I think we don't need all that magic, not in this case, we have two asynchronous calls handlers(functions), no matter which end first, we need to know when the second end, then all we need is a third function that will be called by the two handlers and act when all the data is ready. BTW我认为我们不需要那么神奇,不是在这种情况下,我们有两个异步调用处理程序(函数),无论哪个结束,我们需要知道第二个结束时,那么我们所需要的只是第三个函数这将由两个处理程序调用,并在所有数据准备就绪时执行。 I know this approach will vaste four or five lines more of code than the elegant jquery solution, but at the end our brain and soul will be in better condition. 我知道这种方法将比优雅的jquery解决方案更多地消耗四到五行代码,但最终我们的大脑和灵魂将处于更好的状态。 Sorry my english. 对不起我的英文。

Put the barSuccess call in the fooSuccess success callback. barSuccess调用放入fooSuccess成功回调中。

fooSuccess(data){
  jQuery.ajax({
    data: data,
    success: function(response){
      barSuccess(data) //should still be in scope, I think?
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM