getUsers(); function getUsers(){ console.log('Start'); $.ajax({ url: 'https://jsonplaceholder.typicode.com/users', method: 'GET', dataType: 'json', success: function (data) { data.forEach(user => { console.log(user.id); }); }, error: function () { console.log(data.status); } }) console.log('End'); } The above code-example should print the following: Start ids ... End
However it has the following problem: The order of printing of the data is wrong. It prints Start End data-object
I know it has to do with async, however I can't fix the problem. I tryed alot of things like $.When() Promise async/await But none of them helped.
So could you please show how to fix this problem?
Thank you in advance
It prints Start End data-object- because the firstainajaxstands for asynchronous - so, yeah,endis logged before the request completes - that's asynchrony.done(() => console.log("End"))like in this answer would be what I would do but there are many other options