1

i am testing Phonegap Web Storage for a small project:

var listsCount = 0;

tx.executeSql("SELECT * FROM list WHERE id = ?", [id], successGetList, errorGetList );

function successGetList(tx, results){
    listsCount = results.rows.length; // this will be 2, in my case
}

function errorGetList(err){
    console.log("Error processing SQL: "+err.code);
}

console.log(listsCount); // this will show 0, instead of 2

the issue i'm having is that listsCount doesn't get set inside the successGetList method. Even if i return it there.

any ideas on how can i set that variable inside of successGetList function ?

thanks

Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
  • Possible duplicate of [How to return the response from an AJAX call from a function?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call-from-a-function) (Granted, this not AJAX, but the same asynchronous behavior applies.) – Frédéric Hamidi Feb 18 '13 at 18:54
  • @FrédéricHamidi This is not asking about `return` either. You're right that the answer there should point the right direction, but maybe that's too much for this particular question. – bfavaretto Feb 18 '13 at 19:00

1 Answers1

4

It does get set, but your console.log call is being fired before that (that's how asynchronous code works!). Just use the value from the callback. If you need, call another function from there, and pass your data to it.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150