Skip to content

Instantly share code, notes, and snippets.

@fernandoc1
Created November 3, 2014 14:57
Show Gist options
  • Save fernandoc1/aa4ea549463c8c3854f3 to your computer and use it in GitHub Desktop.
Save fernandoc1/aa4ea549463c8c3854f3 to your computer and use it in GitHub Desktop.
Simple jQuery promise test.
<html>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
function getData()
{
var deferred = $.Deferred();
var xhr = new XMLHttpRequest();
xhr.open("GET", "test.txt", true);
xhr.addEventListener('load', function()
{
if(xhr.status === 200)
{
deferred.resolve(xhr.response);
}
else
{
deferred.reject("Error!!!");
}
}, false);
xhr.send();
return deferred.promise();
}
promise = $.when(getData());
promise.done(function(data)
{
document.getElementById("output").innerHTML = "Data got: "+data;
});
});
</script>
<pre id="output"></pre>
</body>
</html>
@fernandoc1
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment