Tenho o método:
RepoApi.prototype.getContributors = function() {
var returnList = [];
$.get(this.url).done(function(response) {
console.log(response);
returnList = response;
}).fail(function(response){
console.log(response);
});
return returnList;
};
Porque o $.get
assíncrono está sempre retornando a lista vazia?
Como resolver isso?
Edição:
<!DOCTYPE html>
<html>
<head>
<title>
Demo - RepoAPI.js
</title>
<script src="https://code.jquery.com/jquery-2.2.3.js" integrity="sha256-laXWtGydpwqJ8JA+X9x2miwmaiKhn8tVmOVEigRNtP4=" crossorigin="anonymous"></script>
<script src="app.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var url = "https://api.github.com/repos/Spring-AL/demo-spring-thymeleaf/contributors";
var repoApi = new RepoApi(url);
var listContributors = [];
repoApi.getContributors(function(err, resp) {
listContributors = resp;
});
console.log(listContributors);
for (var i = 0; i < listContributors.length; i++) {
console.log(listContributors[i].login);
}
});
</script>
</head>
<body>
</body>
</html>
Console: