1

Here is my code:

 initHomeFeed: function(options) {
  $.get('/feed_items', {}, function(data) {
      console.log("Y U NO COME HERE")
      console.log("hello");
      Feed.feed_items_html = data.feed_items_html_array;
      Feed.pseudo_feed_items_html = data.pseudo_feed_items_html_array
      console.log(ordered_arr);
    }, "json");

  console.log("makehtml was called by inithomefeed")
  Feed.makeHomeHTML();

"makehtml was called by inithomefeed" is printed to the console, and so are the console logs inside the makeHomeHTML function. However, none of the console logs inside the get function have been displayed. How can I make it so it doesnt skip the get function?

Jessica Shu
  • 256
  • 3
  • 12

2 Answers2

6

It's not skipping the get function. The get function is async, so the callback isn't executed until the response is received by the client. The console log's would be printed out if there aren't any issues with the request.

If you are not sure why or if the request is successful, you could inspect the network tab in chrome's developer tools, or some sort of http proxy tool like fiddler to see what the error is.

Justin Bicknell
  • 4,804
  • 18
  • 26
0

If you still want to execute the code

console.log("makehtml was call

after the server call, you can use jQuery ajax with async set to false.

Follow example at How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

But this comes with its own disadvantages.

Community
  • 1
  • 1
Swadeesh
  • 576
  • 1
  • 4
  • 23