4

I wrote the following function to check if my HTML5 openDatabase table is filled or empty:

var that = this;
that.db = openDatabase('dbname', '1.0', "description", 1024 * 1024);

that.tableFilled = function( tableName ) {

    that.db.transaction(function ( tx ) {

        tx.executeSql('SELECT * FROM ' + tableName, [],
            function success( c, results ) {
                return ( results.rows.length > 0 ? true : false );
            },
            function fail() {
                console.log('FAiL');
            }
        );

    });

};

I am trying to return the true or false values to tableFilled().

Actually that.tableFilled('tableName') returns undefined.

What I am trying to achieve at the end is:

if ( that.tableFilled('tableName') ){
    // ...
}

Is there a way I can return the true or false values to the parent function tableFilled() without using a callback ?

Pierre
  • 18,643
  • 4
  • 41
  • 62
  • question in return: WHY don't you want to use a CB? Javascript offers the power to return functions to write async code, why not use it? – Christoph Feb 10 '12 at 21:08
  • 2
    Short answer: **no**, because it's asynchronous. I'll elaborate a bit more in an answer. – Halcyon Feb 10 '12 at 21:08
  • The anonymous function (for potential async call) does seem a little pointless to be honest though for this use-case, as you can't really proceed any further until you have your answer. – twilson Feb 10 '12 at 21:10
  • Just create a normal function in this case. – twilson Feb 10 '12 at 21:11
  • 1
    @Christoph I am currently using a callback in my project. I was wondering if this could be done the way described above. – Pierre Feb 10 '12 at 21:17

1 Answers1

5

You're dealing with an asynchronous process so you can't return a value directly.

What you can do however is return a promise. Your function will promise to give you that value when it's available. To get the value out of the promise, you have to add a callback function.

You still need to use a callback function but you don't need to nest your functions anymore, you can just serialize them.

This may be way out of scope for your current needs but it's a very interesting concept. Just Google for it if you want to know more.

Here is a short example:

function my_function() {
    var promise = new_promise();
    do_asynchronous(function callback(result) {
        promise.resolve(result); // gets called after 1 second
    });
    return promise;
}

var promise = my_function();
promise.done(function(result) {
    console.log(result);    // prints "yay!" after 1 second
});

function new_promise() {
    var handlers = [];
    return {
        "resolve": function (result) {
            for (var i = 0; i < handlers.length; i += 1) {
                handlers[i](result);
            }
        },
        "done": function (a_callback) {
            handlers.push(a_callback);
        }
    };
}

function do_asynchronous(callback) {
    setTimeout(function () {
        callback("yay!");
    }, 1000);
}
Halcyon
  • 57,230
  • 10
  • 89
  • 128