0

First, I know this is conceptually a duplicate question. I went through this one and I really didn't understand how/where to do the callback in my case.

In the code below what I want to do is return the Value of Agents

function CheckWebChatAvailability() {
    var Agents = 0;
    $.get(baseURL + url2, function(data, status) {
        if (status == "success") {
            $.post(baseURL + url, queuequery, function(data, status) {
                Agents = parseInt(data.queue.agentsAvailable);
            }); // end $.post for Server1
        } // end if
        else {
            $.get(baseURL + url2S2, function(data, status) {
                $.post(baseURL + urlS2, queuequery, function(data, status) {
                     Agents = parseInt(data.queue.agentsAvailable);
                }); // end $.post for Server2
            }); // end $.get for Server2
        } // end else
    }); // end $.get for Server1
} // end function CheckWebChatAvailability

ADDED: I want to return the value of Agents so that I can do something like show/hide a div based on the result. I don't want to do that inside CheckWebChatAvailability because it may be called from differents part of the code.

So I want to do, say, var myAgents = CheckWebChatAvailability(); or if that is not possible, var myAgents = newFunctionThatCallsCheckWebChatAvailability()

Community
  • 1
  • 1
Amarundo
  • 2,357
  • 15
  • 50
  • 68
  • Return the values of agents **to what**? In javascript AJAX, you typically do something with your data in an anonymous function (which you have) that passes the value to any other relevant parts of your code. So, at this point, we can't answer until you clarify: return the values of Agents to what / where? – random_user_name Dec 02 '16 at 15:50
  • 1
    What is used here are closures. And (as far as I know) you cannot return anything. It would not make any sense to do this, as these are asyncron. If you need to do something, you must do that in you closure. (Which can call other functions defined or even use an event to do whatever you want with Agents. – Seb Dec 02 '16 at 15:52
  • @Seb's point is valid. Given that these are asynchronous, your `CheckWebChatAvailability` function will return before those http requests are completed. Thus you cannot return anything from the result of those http requests from your original function. – sid-m Dec 02 '16 at 15:56
  • @Seb, I though that is how callbacks could help – Amarundo Dec 02 '16 at 15:58
  • I know this question has an answer - that's how I started my post, pointing to the same article. I just didn't understand it, and I'm asking for help. – Amarundo Dec 02 '16 at 15:59

0 Answers0