0

I need your help to create a SQLite class which allows send a query and returns the result.

I know transaction / executesql are asynchronous and I am trying to resolve this problem.

I wrote this code:

function SQLite(pName){
    this.result = null;

    //External function
    this.Query = function(pQueryStr) {
        this.result = null;
        execQuery(pQueryStr);
        //Waiting query result. While is null, sleep...
        while(this.result == null){null;} //This line doesn't work
        return this.result;
    }

    //Internal function for execute query
    function execQuery(pQueryStr) {
        //Emulating transacion-executesql functions with lag
        setTimeout(function(){
            this.result = "my query result";
        }, 3000);
    }
}

db = new SQLite("dbName");
var res = db.Query("query request string");
//As I can't wait for SQL result, 'res' returns null.
alert("External result: " + res);

This doesn't work but commenting 'while' line... this works adding this code to end.

setTimeout(function(){
    alert("External result (with lag): " + this.result);
}, 5000);

My problem: I need 'while'. This make function waits for query result is reported.

Any solution or workaround?

Thanks for your time!

0

1 Answer 1

1

I would suggest using callbacks or promises the latter is what I would prefer https://www.promisejs.org/ is a good place to start.

In case you still insist on using while (it is bad, because your application will hang until result is back). Your while loop does not work

setTimeout(function(){
        this.result = "my query result";
    }, 3000);

because this context has changed (more about this here: http://ryanmorr.com/understanding-scope-and-context-in-javascript/), and you either have to declare this property in outer scope, or bind this context

function execQuery(pQueryStr) {
    var that = this;
    //Emulating transacion-executesql functions with lag
    setTimeout(function(){
        that.result = "my query result";
    }, 3000);
}

also you need to make recursive check instead of while, example:

var that = this;
function checkResult() {
    if (that.result == null) {
        console.log('repeat')
        setTimeout(checkResult,1);
    }
    else {
        console.log('success');
    }
}
checkResult();
setTimeout(function() { that.result = true; },100)
Sign up to request clarification or add additional context in comments.

4 Comments

I think main problem is one-thread property in Javascript. My app doens't work because execQuery cannot start execution until 'while' has finished. I was looking a one-function solution but I suspect it isn't possible as I would need two functions.
You are right. That is why i suggest using either promises or callbacks. Instead of while loop you can make recursive setTimeout for not blocking the app
added an example of recursive check instead of while loop
Recursive check has same problem as 'while'. I have could solve my problem, partially, using callbacks or events. I say 'partially' because 'asyncronism' + 'unique-thread' have influence over MVC and OOP design. In any case, thanks for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.