Skip to content

Instantly share code, notes, and snippets.

@tmpaul06
Created July 3, 2017 07:45
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 tmpaul06/62bce54d2994712f8b3eef40e281a657 to your computer and use it in GitHub Desktop.
Save tmpaul06/62bce54d2994712f8b3eef40e281a657 to your computer and use it in GitHub Desktop.
Promise deferred
function getSomeJSON(url, callback) {
// AJAX call to get data from server. Accepts callback and calls
// with callback(err, response)
...
}
function fetchJSON(url) {
var deferred = new Deferred();
getSomeJSON(url, function (err, response) {
if (err) return deferred.reject(err);
deferred.resolve(response);
});
return deferred.promise;
}
fetchJSON("http://www.google.com").then(..);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment