You're probably not going to be able to do that as you have things now. With JS being by default asynchronous, rows will be returned even before any of those callbacks run. Since the success callback is what sets rows, you'll always be returning an either unset or stale value.
(Note: i have never used html5sql. It might be that the library just presents an interface that looks async while somehow actually working synchronously. But if it does, it'd be quite unusual in that regard.)
One possible fix would be to take a callback yourself, that you call and pass the rows to once you get them.
function myFunction(callback){
html5sql.process(
["SELECT * FROM "+table],
function(transaction, results, rowsArray){
callback(rowsArray);
},
function(error, statement){
});
}
// and use it like this
myFunction(function(rows) {
// do stuff with rows in here
});
Or, just use callback as the second arg to process, if you want to be lazy. Just know that it will be passing all three args, which the caller of this function shouldn't have to care about. :P
var rows;. The way it is now,rowsis defined only within the scope of the inner function, so once it gets back out to where you're trying to return it, it doesn't exist anymore. By declaring it before you assign anything to it, you're ensuring that the variable namedrowsthat is having values assigned to it is the one that can be returned.