Created
November 4, 2014 11:51
-
-
Save tobiashm/8d724b4f981c5462c80a to your computer and use it in GitHub Desktop.
Wrap jQuery Ajax in real Promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| function AjaxError(jqXHR, textStatus, errorThrown) { | |
| this.name = 'AjaxError'; | |
| this.message = textStatus; | |
| this.jqXHR = jqXHR; | |
| this.errorThrown = errorThrown; | |
| } | |
| AjaxError.prototype = new Error(); | |
| AjaxError.prototype.constructor = AjaxError; | |
| function wrapAjax(jqPromise) { | |
| return new Promise(function(resolve, reject) { | |
| jqPromise.then(function(data, textStatus, jqXHR) { | |
| resolve(data); | |
| }, function(jqXHR, textStatus, errorThrown) { | |
| reject(new AjaxError(jqXHR, textStatus, errorThrown)); | |
| }); | |
| }); | |
| } | |
| Promise.wrapAjax = wrapAjax; | |
| })(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want access to the metadata on success, just do:
and in the consumer code, something like: