2

I'm using an anonymous function to perform some work on the html I get back using Restler's get function:

var some_function() {
    var outer_var;
    rest.get(url).on('complete', function(result, response) {
        if (result instanceof Error) {
            sys.puts('Error: ' + result.message);
        } else {
            var inner_var;
            // do stuff on **result** to build **inner_var**
            outer_var = inner_var;
        }
    });
    return outer_var;
}

How can I get the value of inner_var out to the some_function scope and return it? What I have written here doesn't work.

0

3 Answers 3

6

The get call is asynchronous, it will take some time and call your callback later. However, after calling get, your script keeps executing and goes to the next instruction. So here is what happens:

  • you call get
  • you return outer_var (which is still undefined) ... sometimes later ...
  • get result has arrived and the callback is called.
  • outer_var is set

You can't have your some_function return a value for something that is asynchronous, so you will have to use a callback instead and let your code call it once data is processed.

var some_function(callback) {
    rest.get(url).on('complete', function(result, response) {
        if (result instanceof Error) {
            sys.puts('Error: ' + result.message);
        } else {
            var inner_var;
            // do stuff on **result** to build **inner_var**
            callback(inner_var);
        }
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, the callback was the way to do it. I do have a question though. The .on('complete', function(... was supposed to take care of the asynchronous nature of the call, right? The code in that function should not have executed until the get call finished. Would I somehow still have been able to pass data out of the function handler back out to the outer context?
When you declare the callback for on "complete", you create a closure: the function is created and its scope contains everything declared above. So yes, the callback can pass data to the outside this way. You should definitely try to execute the code step by step in the debugger in order to understand the order in which functions are called, and what is the scope accessible for each function. It will make things much clearer than this explanation :)
2

Like most of the modules of node, Restler is also a event based library having async calls. So at the time you do return outer_var; the complete callback was not necessarily called (With some libraries it could be if it the result was cached, but you should always expect that it is called asynchronous).

You can see this behavior if you add some logging:

var some_function() {
    var outer_var;
    console.log("before registration of callback"); //<----------
    rest.get(url).on('complete', function(result, response) {
        console.log("callback is called");  //<----------
        if (result instanceof Error) {
            sys.puts('Error: ' + result.message);
        } else {
            var inner_var;
            // do stuff on **result** to build **inner_var**
            outer_var = inner_var;
        }
    });
    console.log("after registration of callback");  //<----------
    return outer_var;
}

So if you would like to do something with this value you would need to call the function that should do something with this value out of your complete callback.

Comments

-3
  1. Remove the var outer_var; from your function some_function;
  2. Declare the var outer_var; upper then your function

It should be work after you did step 1, not really need step 2.

1 Comment

The problem is because the call of complete callback of get is asynchronous.

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.