I strongly recommend that you do not work synchronously with AJAX. Synchronous requests are obsolete and this makes a lot of sense, in one language Single-Thread, competing with renderings of HTML and CSS in the browser, the use of synchronous requests tend to be a bad practice (I don’t see a case that’s seen as a good idea, maybe someone knows, but to this day I’ve NEVER seen).
You can tailor your code to work asynchronously easily:
RepoApi.prototype.getContributors = function() {
return $.ajax({
method: 'GET',
url: this.url
});
};
Now just use the method correctly in the place where it is called, for example:
var repo = new RepoApi();
repo.getContributors()
.done(function(response) {
console.log(response);
}).fail(function(response){
console.log(response);
});
Working with AJAX asynchronously is the ideal way, but if you are dissatisfied with the way you have to work with asynchronous (using Promises), you have two other options:
Study a little deeper: Promises (what jQuery implements in this case with the #done #fail methods, although it does not follow the ECMA-defined interface) and Generators, then take a look at the idea of task js. Several libraries have mechanisms to work with this idea, the Q.js has a function (Q.Spawn) to the same idea of task js.
Study for async-await, an ES7 Feature that solves all these problems in a simple and elegant way. There are some pre-processors that allow the use of async-await, that can even be solved with the idea of task js.
I made the change in my getContributors method. but the error persists. I will edit it for you to see how I am trying to access the method.
– Jose Vieira Neto
@Josevieiraneto do not change the question. Create a jsFiddle to see the code
– Sergio
guy already edited.. I will but create jsFiddle
– Jose Vieira Neto
@Josevieiraneto the problem is that all the code has to be inside that callback, the
console.log
and thefor
next.– Sergio
@Have you seen my comment? The code should look like this: https://jsfiddle.net/xb9z1xcg/
– Sergio
Thanks, it worked out :)
– Jose Vieira Neto