Skip to content

Instantly share code, notes, and snippets.

@morganney
Last active August 29, 2015 13:55
Show Gist options
  • Save morganney/8745938 to your computer and use it in GitHub Desktop.
Save morganney/8745938 to your computer and use it in GitHub Desktop.
Function to fetch a Gist using jQuery's $.ajax and JSONP.
// JSONP is required since gists.github.com doesn't support CORS.
// Note that it returns a Promise.
function fetch(gistid) {
return new Promise(function(resolve, reject) {
$.ajax('https://gist.github.com/' + gistid + '.json', {
dataType: 'jsonp',
cache: true
}).done(function(gist, status, jqXhr) {
// You can modify the gist before resolving.
resolve(gist);
}).fail(function(jqXhr, status, error) {
var e = new Error('Unable to retrieve gist ' + gistid);
reject(e);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment