Advertisement
enyo

ajax-promise

Dec 8th, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ajaxPromise = function(params) {
  2.   var deferred = Q.defer(); // Create the promise
  3.  
  4.   $.ajax(params) // Create the call normally
  5.   .error(function(err) {
  6.     deferred.reject(err); // Causes ths promise to fail
  7.   })
  8.   .success(function(result) {
  9.     deferred.result(result); // You probably want to standardize your results here
  10.   });
  11.  
  12.   return deferred.promise;
  13. };
  14.  
  15. // You can then create shortcut methods for post, get, etc...
  16. ajaxPromise.post = function(params) {
  17.   params.type = "POST";
  18.   return ajaxPromise(params);
  19. };
  20. // etc...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement