Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Library to Synchronize Events

let's say I'm doing 3 ajax calls and I want to wait for the 3 calls to finish before doing something.

Is there a library out there to synchronize multiple async events in JavaScript ? (using or not jQuery's event system)

Ex.:

var sync = new syncLib();
$('a1').click(sync.newListener());
$('a2').click(sync.newListener());

sync.wait(function(e1, e2) {
  // fired when both a1 and a2 are clicked or when 10 seconds have passed
  // e1 and e2 would have properties to know whether or not they timed out or not..
}, 10 /* timeout */));

I have found this one: https://github.com/Ovea/js-sync/blob/master/README.md, but timeouts are not supported. (Let's say the second ajax call takes too long, I don't want my synchronization to be hung up, I want to set a 10 secs timeout)

I know I can code something myself, but I'm just checking here (after googling for it)

Thanks!

EDIT: Since then I found async: https://github.com/caolan/async

like image 966
Mike Gleason jr Couturier Avatar asked Dec 04 '22 18:12

Mike Gleason jr Couturier


2 Answers

$.when($.ajax("/"), $.ajax("/"), $.ajax("/")).then(function () {
    alert("all 3 requests complete");
});

Documentation

like image 101
Esailija Avatar answered Dec 29 '22 00:12

Esailija


you can use jquery deferred object

here is a useful post http://www.erichynds.com/jquery/using-deferreds-in-jquery/

like image 39
Rafay Avatar answered Dec 28 '22 22:12

Rafay