Skip to content

Instantly share code, notes, and snippets.

@ianmstew
Last active September 18, 2015 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianmstew/bf64793e37593a37eecb to your computer and use it in GitHub Desktop.
Save ianmstew/bf64793e37593a37eecb to your computer and use it in GitHub Desktop.
var RSVP = require('rsvp');
var jQuery = require('jquery');
// Log uncaught errors (important)
RSVP.on('error', function (reason) {
console.error((reason && reason.stack) || reason || 'Error');
});
// Shim the browser window with ES6 Promise
window.Promise = RSVP.Promise;
// Replace jQuery ajax to return an ES6 Promise
jQuery.ajax = function () {
var ajaxArgs = arguments;
// Return an ES6 Promise rather than a jQuery xhr
return new Promise(function (resolve, reject) {
Backbone.$.ajax.apply(Backbone.$, ajaxArgs)
.done(function (data, textStatus, jqXHR) {
resolve(data);
})
.error(function (jqXHR, status, error) {
// Always resolve errors to JSON
reject(jqXHR.responseJSON || { error: jqXHR.responseText || jqXHR.statusText });
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment