Saturday, September 21, 2013

.then()

jQuery's deferred stuff allows you to chain a .then() onto things like so:

$.ajax({
   type: "POST",
   url: '/whatever/',
   dataType: 'json',
   success: function (result) {
      alert('fires first assuming a synchronous scenario');
   }
}).then(function () {
   alert('fires next assuming a synchronous scenario');
});
alert('fires first assuming an asynchronous scenario');

 
 

By the way, I am bringing this up because I am struggling to find a good way to force an asynchronous call to resolve before proceeding procedurally. If you are in control of the call and it is not something in another library getting wrapped by your logic, you can always just specify async: false like so:

$.ajax({
   async: false,
   type: "POST",
   url: '/whatever/',
   dataType: 'json',
   success: function (result) {
      alert('fires first assuming a synchronous scenario');
   }
}).then(function () {
   alert('fires next assuming a synchronous scenario');
});
alert('fires first assuming an asynchronous scenario');

No comments:

Post a Comment