The return of function $.get
is a promise, so you can use the method done
whenever you want to use the return obtained. For example, below I did that as soon as the page loads the list of tweets is displayed on the screen using the method done
. In this case, it is equivalent to using the anonymous function as callback.
You can read more here: What to use in Ajax, Success or done?
The point is that when pressed the button, the same tweets are listed in an orderly fashion. To simplify, I have sorted the value of the id
returned by the server. Note that in the event handling click
I used the method again done
promise. This method works like this: given an asynchronous event, in this case the request, the method done
invokes the function callback passed to it when the event is completed; if a new function is passed callback after the event is completed, the function is promptly executed. In this example, to list the tweets, the function will wait for the request to be completed and, when pressing the button, the sort will be executed immediately, because the request would have already been completed.
In fact, even if the request has not yet been finalized when pressing the button the operation would be the same as the functions callback, both to list and to sort, would be invoked in this order, generating the same result.
$(() => {
const ul = $("ul");
const button = $("button");
const tweets = $.get("https://api-nave-twitter.herokuapp.com/tweets");
tweets.done(function (data) {
$(data).each(function (index, tweet) {
ul.append($("<li>").text(tweet.text));
});
});
button.on("click", function (event) {
tweets.done(function (data) {
let sorted = data.sort((a, b) => a.id <= b.id);
ul.empty();
$(data).each(function (index, tweet) {
ul.append($("<li>").text(tweet.text));
});
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul></ul>
<button>Ordenar por id</button>
Ask this question: https://answall.com/q/27177/5878
– Woss
return an object with several fields over the answer, I realized that what I want stays in x as responseJSON, to get the only this array is to assign x.responseJSON to a variable?
– Felipe Adamoli
Felipe, what you’re doing is an asynchronous call, so the JS doesn’t know when the request will be completed and thus doesn’t know the value that should be returned to
x
. What is returned, in fact, is a promise (from English, Promise) which can be used to treat the response, but you already do this in your anonymous role. Instead of returning the value to the variable, implement the logic within its anonymous function, using the data indata
. If you didn’t understand something I said, I suggest you read the reading I mentioned before.– Woss
I understood yes, it is that I needed to reuse the object later in another function, and my thought was to make the request only once. I do the get, get a list and print it on the screen immediately. If the user clicks on a button he uses that same list, sorts it and reimpresses it only in order on the screen. It is best to request it twice?
– Felipe Adamoli
Felipe, if you need to call another function call, but do it from within the callback (from what you already have or from the promise .done). I would only not recommend storing the value in an external scope variable.
– bfavaretto
I could tell you what doesn’t work?
– Sam
@Felipeadamoli You can request only if the value is not, say,
null
.– Pablo Almeida