开发者

Extjs: two parallel ajax call

开发者 https://www.devze.com 2023-01-31 08:25 出处:网络
my code creates two 开发者_JS百科ajax call at the same time (i assume the parallelism would be more efficient). I want to load a table if both calls succeed. What\'s the proper way of doing this?var s

my code creates two 开发者_JS百科ajax call at the same time (i assume the parallelism would be more efficient). I want to load a table if both calls succeed. What's the proper way of doing this?


var succeeded = {};

function callBackOne(){
     succeeded.one = true;
     // your other stuff
     if (succeeded.two) { bothHaveSucceeded());
}


function callBackTwo(){
     succeeded.two = true;
     // your other stuff
     if (succeeded.one) { bothHaveSucceeded());
}


I'd use a delayed task personally:

var success = {
  one: false,
  two: false
};

// Task
var task = new Ext.util.DelayedTask(function(){
   // Check for success
   if (success.one && success.two) {
      // Callback
      doCallback();
   } else {
      task.delay(500);
   }
});
task.delay(500);

// First
Ext.Ajax.request({
   ...
   success: function() {
      success.one = true;
   }
   ...
});

// Second
Ext.Ajax.request({
   ...
   success: function() {
      success.two = true;
   }
   ...
});

The task acts like a thread and will check on the status of the requests and sleep for every 500ms until they both complete.


Old question, but well, as I stumbled upon it...

I'd use the excellent async library by Caolan, particularly here you'll want to use async.parallel.

The examples written on the GitHub doc are worth a read.

https://github.com/caolan/async#parallel


Share an integer variable that each callback checks:

// count variable
var numReturns = 0;

// same call back used for each Ajax request:
function callback() {
    numReturns++;
    if (numReturns === 2) {
        progress();
    }
}

If you need different callbacks, have each callback fire an event which does the same thing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号