Created
February 18, 2015 10:33
-
-
Save ayozebarrera/21d00670236b16c753a2 to your computer and use it in GitHub Desktop.
json call with promises
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
if (window.Promise) { | |
console.log('Promise found'); | |
var promise = new Promise(function(resolve, reject) { | |
var request = new XMLHttpRequest(); | |
request.open('GET', 'http://api.icndb.com/jokes/random'); | |
request.onload = function() { | |
if (request.status == 200) { | |
resolve(request.response); // we got data here, so resolve the Promise | |
} else { | |
reject(Error(request.statusText)); // status is not 200 OK, so reject | |
} | |
}; | |
request.onerror = function() { | |
reject(Error('Error fetching data.')); // error occurred, reject the Promise | |
}; | |
request.send(); //send the request | |
}); | |
console.log('Asynchronous request made.'); | |
promise.then(function(data) { | |
console.log('Got data! Promise fulfilled.'); | |
document.getElementsByTagName('body')[0].textContent = JSON.parse(data).value.joke; | |
}, function(error) { | |
console.log('Promise rejected.'); | |
console.log(error.message); | |
}); | |
} else { | |
console.log('Promise not available'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment