0

I am using a synchronous request in my code to get a value which is needed in the next function for my app to work correctly. The synchronous request is however depreciated therefore should not be used.

$.ajax({type: 'POST',
        url: 'ins-id.php',
      success: function(data) {
        Rsp = JSON.parse(data);
        if (Rsp.success) {Key = Rsp.id};
    },
    async:false
    });

The reason given for the depreciation is that synchronous requests cause delays and thus disrupt the user experience.

Thus my question is, what is the prescribed method to retrieve data which is needed for immediate consumption?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 3
    The word is "deprecated." It's not about an asset decreasing in value over time. And the `success` function is what handles the response from your `$.ajax()` call. Everything after the `$.ajax()` will run immediately, which is why you need to put your "immediate consumption" logic in the `success` function. Otherwise, you won't be sure the response has been returned. `success` waits for the response. – Marc Nov 01 '15 at 18:33
  • Thanks Marc, I appreciate that. However, "deprecated" does mean that there is the intent to remove the function. My query is aimed at obtaining a better method to achieve this or a method more consistent with the implied design methodology. – dies_felices Nov 01 '15 at 18:41
  • 3
    This is already answered [in SO thread 16511682](http://stackoverflow.com/questions/16511682/i-need-help-finding-an-alternative-to-synchronous-jquery-ajax). If this **does not** answer your question, please tell us what the difference is. – Kevin Ard Nov 01 '15 at 18:44
  • 3
    Worth reading [How to return value from asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call). Bite the bullet and learn how to program asynchronously as it's a very important aspect of Javascript programming. – jfriend00 Nov 01 '15 at 18:48
  • Thanks Kevin ard, jfriend00. Both yours and Marc's answers have indeed addressed my query. I would suggest that there may be fringe cases in which encapsulating your problem code in in the $.ajax() asynchronous request may not be practical. As an aside many real world applications of asynchronous systems employ tools to work around the hazards of not knowing when you're going to get an answer, Semaphores and Request - Acknowledge being among them. – dies_felices Nov 01 '15 at 19:10

0 Answers0