0

I have the following code:

var ids = new Array(5);
$(function(){
html5sql.process(sql, 
    function(transaction, results, rowsArray){
        for(var i = 0; i < rowsArray.length; i++){
            ids[i] = rowsArray[i].ID;
                            console.log(ids[i]); //returns the actual value for i
        }
    },
    function(error, statement){
              console.log("Error: " + error.message);       
            }
);
console.log(ids[0]); //returns undefined
});

As you can see, the print out inside the for loop prints the correct value. On the other hand, when I print outside the function I get undefined. What is the problem with the scoping? How can I get the actual value of the array outside the function?

Thanks

4
  • 4
    Welcome to the wonderful world of async! You can't do that. Commented Jan 5, 2012 at 1:53
  • Those functions are callbacks. They are not invoked immediately. Commented Jan 5, 2012 at 1:56
  • How can I solve this problem. Can anyone please explain to me what is happening? Commented Jan 5, 2012 at 1:58
  • You are being told that the call to html5sql.process() runs asynchronously. The call to console.log() occurs immediately afterward and before any values are assigned to ids. You need to delay the call to console.log until processing is finished (and values have been assigned). I.e. you have a timing issue, not a scoping issue. Commented Jan 5, 2012 at 2:20

1 Answer 1

3
var ids = [];

$( function () {

    html5sql.process( sql, function () {

        // LINE A: you are populating your array here

    });

    // LINE B: the array is still empty here

});

LINE A does appear above LINE B in the source code, but that doesn't necessarily mean that it executes sooner. That is because LINE A is inside a function expression which is passed into the process function. This process function will invoke that function expression at some time in the future.

So, basically, LINE B is executed immediately, and LINE A is executed at some point in the future. The function expression is probably bound (as a handler) to a SQL-related event.

Sign up to request clarification or add additional context in comments.

8 Comments

I think your explanation is a bit flawed. The code is executed in order, but the function called by html5sql() is being run asynchronously (apparently) so it hasn't assigned values to ids when the call to console.log is made immediately afterward.
@RobG Could you be more specific? What statement from my answer is problematical?
The part that says LINE A does appear above LINE B in the source code, but that doesn't necessarily mean that it executes sooner. The code is executed in sequence, but the function it calls is asynchroonous so it's still executing when the next line is called.
@RobG My statement is correct. LINE A is not executed immediately, and LINE B is executed immediately. Therefore LINE B is executed sooner (in time) than LINE A. As a matter of fact, from the code provided by the OP, he have no guarantee that LINE A is executed at all. When the statement html5sql.process(...); executes, the function expression is passed into the process function - the function body of that function expression is not executed at this point. It's up to the process function to decide if it wants to invoke that function immediately...
...or bind it as an event handler so that it's invoked when an event triggers at some point in the future. Therefore, my statement - "LINE A does appear above LINE B in the source code, but that doesn't necessarily mean that it executes sooner (in time)" - is correct.
|

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.