0

I am just really stuck when it comes to returning a function inside another function then inside another function. All I want it to do is send a user Id to getUser then return to postFeed or put it in authorOfPost. So I can put it in a p tag.

  function postFeed(a){
  $('.loginForm').remove();

  for(i=0;i<a.data.length;i++){
    getUser(a.data[i].user.id);
    console.log(authorOfPost);
    if ((a.data[i].hasOwnProperty("title") && a.data[i].hasOwnProperty("text"))&&(a.data[i].title !="" && a.data[i].text !=""))
    {
      $('.body').append('<div class="post" ><h4 class="date-title">'+ a.data[i].display_time +'</h4><h1 class="title">'+ a.data[i].title + '</h1><p class="content">'+ a.data[i].text+'</p><p class="author">'+authorOfPost+'</p></div>' );
    }
    else if (a.data[i].hasOwnProperty("title")&& a.data[i].title !="") {
      $('.body').append('<div class="post"><h4 class="date-title">'+ a.data[i].display_time+'</h4><p class="content">'+ a.data[i].title+'</p><p class="author">'+authorOfPost+'</p></div>' );
    }
    else if (a.data[i].hasOwnProperty("text") && a.data[i].text !="") {
      $('.body').append('<div class="post"><h4 class="date-title">'+ a.data[i].display_time+'</h4><p class="content">'+ a.data[i].text+'</p><p class="author">'+authorOfPost+'</p></div>' );
    }//end of else if
  }//end of forloop
}//end of postFeed
var authorOfPost="";
function getUser(usr){
    var user_info_link=BASE_URL+"/api/user/"+ usr;
    var user_info = new XMLHttpRequest();

    user_info.onreadystatechange = function() {
  if (user_info.readyState == 4 && user_info.status == 200) {
      var myArr = JSON.parse(user_info.responseText);
      authorOfPost=myArr.data.fullname; // really stuck here not sure how to return this each time to postFeed

    }
  };
    user_info.open("GET",user_info_link);
    user_info.setRequestHeader("token",accessTocken);
    user_info.setRequestHeader("Content-Type", "application/json");
    user_info.send();
}
Josh Martin
  • 311
  • 3
  • 18
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – 4castle Jul 26 '16 at 21:08
  • You've got it structured backwards. `postFeed` should be called from inside the callback in `getUser`. This is a relatively common duplicate, and I highly recommend reading the answer. – 4castle Jul 26 '16 at 21:09
  • @4castle Why do you think that? postFeed is already passing in another request from another function and when I tried to use $.ajax for my request I would get an error because I did not how to set the request headers for content-type and token – Josh Martin Jul 26 '16 at 21:15
  • Then you need to make the other function call `getUser` instead of calling `postFeed`. It will take some restructuring, but that's how asynchronous tasks work, they don't return. – 4castle Jul 26 '16 at 21:19
  • Don't pay attention to the `$.ajax` in the duplicate. The answer applies to all asynchronous tasks. You might want to look at the second answer though for a more non-jquery answer. – 4castle Jul 26 '16 at 21:22

0 Answers0