How to wait till callback function returns?

后端 未结 3 405
悲哀的现实
悲哀的现实 2021-01-19 23:10

I\'m making an android application using phonegap. I\'m using phonegap\'s Storage api for querying a database. here\'s my code:

function directPath(src, dest         


        
3条回答
  •  抹茶落季
    2021-01-19 23:51

    I wrote a function to solve a similar problem, you can call it like this:

    database("SELECT * FROM USER", function(result){
        console.log(result);
    });
    
    //FUNCTION
    function database(sql, callback) {
    
        if(!callback) { callback = function(r) { console.log(r); } }
    
        var database_name = "Database",
            database_version = "1.0",
            database_displayname = "DatabaseName",
            database_size = 1000000,
            result = {
                error: -1,
                message: "",
                len: 0,
                rows: {}
            },
            db = window.openDatabase(database_name, database_version, database_displayname, database_size);
            db.transaction(
                            function (tx) {
                                tx.executeSql(sql, [], querySuccess, errorCB);
                            }, function (tx, results) {
                                //SUCCESS
                            }, function (err) {
                                //ERROR
                            }
            );
            function querySuccess(tx, results) {
                if (results) {
                    result.len = results.rows.length;
                    result.message = "Success";
                    for (var i=0; i

提交回复
热议问题