1

I have a function that calls another function and I want to store the returned result in a variable:

function trigger_gridly(){
    var channel_name = get_channel_name();
}

The called function triggers an AJAX request, which triggers a callback receiving JSON:

function get_channel_name(){
    var channel_name;     
    $.getJSON(APPLICATION_DOMAIN + "channels/new.json?callback=?", null, handleJson);
}

function handleJson(channel){
    var channel_name = channel.name;
    return channel_name;
}

The problem is that the channel_name in trigger_gridly() does not receive the value of channel_name in the handleJson callback function. How can I expose the data in the callback to the trigger_gridly() function?

4
  • You are thinking about this in a synchronous fashion. Doing an asynchronous request and having a simple getter function that returns the value is not possible unless the value has been prepopulated. What exactly are you trying to do? It will likely need to be more callback oriented than your trigger_gridly function. Commented Jan 14, 2012 at 8:55
  • I'm trying to get a json object on the client side from a different domain than the server is on. Commented Jan 14, 2012 at 8:58
  • And do what with it? Perhaps you should pass your function that actually does the processing as the callback. Otherwise, how are you going to know when the variable has been set and when it hasn't been? (Without using something hacky like an infinite loop that breaks once it's set or something.) Commented Jan 14, 2012 at 9:06
  • 1
    possible duplicate of JavaScript asynchronous return value / assignment with jQuery Commented Jan 14, 2012 at 10:10

3 Answers 3

1

As others have indicated, getJSON is an asynchronous call. You have to wait for it to invoke your callback before channel will be available. Here is how I might code it up:

function trigger_gridly() {
   get_channel_name(function(channel){
      var channel_name = channel.name;
      // Do something with channel_name
   });
}

function get_channel_name(success) {
   $.getJSON(APPLICATION_DOMAIN + "channels/new.json?callback=?", null, success); 
}

EDIT: An alternative that I like slightly better:

function trigger_gridly(channel) {
   var channel_name = channel.name;
   // Do something with channel_name
}

function get_channel_name(success) {
   $.getJSON(APPLICATION_DOMAIN + "channels/new.json?callback=?", null, success);
}

get_channel_name(trigger_gridly);
Sign up to request clarification or add additional context in comments.

Comments

0

it's quite ugly, but this will work, asynchronous processes are always a bit tricky getJSON is async.

function get_channel_name(fn) {
    $.getJSON(APPLICATION_DOMAIN + "channels/new.json?callback=?", null, function(channel) {
        fn(channel.name);
    });
}

var channel_name;
get_channel_name(function(name) {
    channel_name = name;
});
// but here channel_name is still undefined

2 Comments

This will work since eventually channel_name will be defined. But it won't be defined where you might expect it to be. Meaning right after calling get_channel_name.
That's what I meant, but I have to apologize, I misread the comment in your code, I thought it was saying it was defined from the point on. Guess it's still too early for me ;) Never mind, I deleted my comment....
0

You need to declare channel_name outside the function scope. However, I am not sure this will get you what you want. This is set up for an async request/response. You want to perform your actions that use the channel name in the handleJson() function.

Like this:

var CHANNEL_NAME;

function trigger_gridly(){
    get_channel_name();
}

function get_channel_name(){
    $.getJSON(APPLICATION_DOMAIN + "channels/new.json?callback=?", null, handleJson);
}

function handleJson(channel){
    CHANNEL_NAME = channel.name;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.