Last active
August 29, 2015 13:55
-
-
Save morganney/8745938 to your computer and use it in GitHub Desktop.
Function to fetch a Gist using jQuery's $.ajax and JSONP.
This file contains 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
// 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