I am trying to return a number that is retrieved through a couple of callback functions, but cannot for the life of me figure out why the scope isn't working. The goal is to return a value that is only accessible inside two additional layers of functions:
function getLastPosition(db) {
var highest; // Declared in method's scope
db.transaction(function (tx) {
tx.executeSql('SELECT max(position) AS highest FROM FRUIT', [], function(tx, result) {
highest = result.rows.item(0).highest;
alert(highest); // Displays the number 2, as it should.
});
});
alert(highest); // Displays 'undefined'!
return highest;
}
I've read Ryan Morr's guide and still come up clueless...
EDIT: So it turns out not to be a problem of scope, but of asynchronous execution. The return statement is executing before the database call is done. My revised question, then, is how to get the value of highest without resorting to more callbacks.