简体   繁体   English

多个同时ajax请求代理

[英]multiple simultaneous ajax requests broker

How to coordinate simultaneous ajax calls and take action only when all of them are completed? 如何协调同时进行的ajax调用并仅在全部完成后才采取措施?

Is there a convenient javascript helper library that will coordinate multiple simultaneous ajax calls to a database and invoke a specified callback when ALL of the ajax data-fetches have returned their data? 是否有一个方便的javascript帮助程序库,该库将协调对数据库的多个同时ajax调用,并在所有ajax数据提取均返回其数据时调用指定的回调? I'd like to submit a dictionary of primed-and-ready-to-go ajax calls to this "coordinator" or "broker", along with my WhenAllCompleted callback function. 我想向我的“协调员”或“经纪人”提交准备好的随时可用的ajax调用的字典,以及我的WhenAllCompleted回调函数。

The use case is a common one: before a database record can be bound to the UI, all of the dropdown lists for the foreign key data relationships have to be populated, so that the user sees meaningful values rather than the integer keys that are typically stored in the record. 用例很常见:在将数据库记录绑定到UI之前,必须填充所有外键数据关系的下拉列表,以便用户看到有意义的值,而不是通常的整数键。存储在记录中。

I would rather not reinvent all the plumbing code to track the status of each ajax request if there's a robust standard library out there for doing it already. 如果已经有了一个健壮的标准库,我宁愿不重新发明所有的管道代码来跟踪每个ajax请求的状态。

EDIT: Here's the pseudo-code that describes what I want to do : 编辑:这是描述我想要做什么的伪代码:

           when ajax1fetch is finished
           and
           ajax2fetch is finished
           and 
           ajax3fetch is finished
           and SELECT1 is populated
           and SELECT2 is populated
           and SELECT3 is populated
           bindRecordToUI()

Before I restructured the code to use $.when() , I was getting the data in the .ajax() success callback, and populating the SELECT right there in that callback; 在将代码重组为使用$.when() ,我要在.ajax()成功回调中获取数据,然后在该回调中填充SELECT。 when the SELECT was populated, then I bind the record to the UI. 当填充SELECT时,我将记录绑定到UI。

If we feed $.when a list of ajax calls, the done callback is invoked when the ajax requests are complete, which occurs before the SELECTs are populated (if the list of items for the SELECT is very long). 如果我们提供$.when ajax调用列表,则在ajax请求完成时调用完成的回调,该完成发生 SELECT填充之前 (如果SELECT的项目列表很长)。

Somehow I need to include the population of the SELECT elements as additional when-conditions, and find a way to route the data returned by the ajax call to the populating functions, if the SELECT-populate functions are not going to be peformed in the .ajax success callback. 我不知何故需要将SELECT元素的填充作为附加的when-conditions,并找到一种方法,以将ajax调用返回的数据路由到填充函数(如果SELECT-populate函数不会在peform中执行)。 ajax成功回调。

You are looking for promises in JS. 您正在用JS寻找承诺 Here is a nice framework Q . 这是一个不错的框架Q。 And the allSettled() function is, what you are looking for. allSettled()函数就是您要寻找的东西。

Q.allSettled([saveToDisk(), saveToCloud()]).spread(function (disk, cloud) {
    console.log("saved to disk:", disk.state === "fulfilled");
    console.log("saved to cloud:", cloud.state === "fulfilled");
}).done();

Code taken from the API-reference 来自API参考的代码

Since you are already using jQuery, you can use the jQuery.when() method to wait for multiple Deferred s to finish. 由于您已经在使用jQuery,因此可以使用jQuery.when()方法等待多个Deferred结束。 Note that jQuery.ajax() (and its wrapper methods, like jQuery.get() ) returns a jqXHR object that is a type of Deferred . 需要注意的是jQuery.ajax()及其包装方法,像jQuery.get()返回一个jqXHR对象,它是一个类型的Deferred

You'll get your Deferred s from your $.ajax or equivalent calls, and then pass those all to $.when . 您将从$.ajax或等效调用中获取Deferred ,然后将它们全部传递给$.when You will get back a Promise , which is basically a read-only Deferred *. 您将获得一个Promise ,它基本上是一个只读的Deferred *。 You can then attach completion/error handlers to this Promise , which will be resolved once all of your passed-in Deferred s are resolved, or is rejected if any one of your Deferred s is rejected. 然后,您可以在此Promise附加完成/错误处理程序,一旦所有传入的Deferred都被解析,则将被解决;如果任何Deferred被拒绝,则被拒绝。

var def1 = $.ajax(),
    def2 = $.ajax({ /* some params */});

var masterPromise = $.when(def1, def2);

masterPromise.done(function() {
    console.log('def1 and def2 are done!');
});

  • A Deferred allows attaching various done and error handlers, as well as resolving and canceling and otherwise controlling the asynchronous action. Deferred允许附加各种完成和错误处理程序,以及解决和取消以及控制异步操作​​的方法。 A Promise only allows the attachment of handlers, and does not allow control over the asynchronous action. Promise仅允许附加处理程序,并且不允许控制异步操作​​。 You can get a Promise from a jQuery Deferred with the Deferred.promise() method. 您可以使用Deferred.promise()方法从jQuery Deferred获得Promise

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

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