Created
May 8, 2013 18:13
-
-
Save teleological/5542360 to your computer and use it in GitHub Desktop.
Reimplementation of http://rubrication.blogspot.ie/2013/05/monads-and-asynchronous-ajax-with.html using jQuery promises
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
var iphost = "http://api.hostip.info/get_json.php"; | |
var countryhost = "http://api.hostip.info/get_json.php?ip="; | |
function getIP() { | |
return $.ajax({ | |
type: 'GET', | |
url: iphost, | |
dataType: 'json' | |
}); | |
} | |
function countryOf(data) { | |
return $.ajax({ | |
type: 'GET', | |
url: countryhost + data.ip, | |
dataType: 'json' | |
}); | |
} | |
function display(data) { | |
console.log(data.country_name); | |
} | |
function showError(xhr, data) { | |
console.log("Error: " + data); | |
} | |
$(document).ready(function () { | |
getIP() | |
.then(countryOf) | |
.then(display, showError) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment