Avinash
Avinash

Reputation: 3

Javascript Promises in SQL Transaction

I am unable to integrate the javascript promises in the web SQL Transaction. I am able to get the log "Test", but I am unable to get the "Query Results" log. Please help me I am new to Javascript. Any help done really appreciated. Below is my code.

function sample()
{
    return executeTransaction()
   .then(function(results)
    {
       console.log("Query Results:");
    });
}

function executeTransaction(query)
{
    console.log("Transaction Query : " + query);

    db.transaction(function(transaction)
    {
        transaction.executeSql(query, [], function(transaction, result)
        {
            console.log("Test: " + JSON.stringify(result));
            return Promise.resolve(result);

        }
        , nullHandler
        , errorHandler);
    });


}

function nullHandler(result)
{
    console.log("Null Log : " + JSON.stringfy(result));

}

function errorHandler(error)
{
    console.log("Error Log : " + error);
}

Upvotes: 0

Views: 1414

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

your executeTransaction function isn't returning anything (much less a promise) - neither is the anonymous function function(transaction)

at first glance, and without knowing too much about the db library you are using, you need to do (at least) this:

function executeTransaction(query) {
    console.log("Transaction Query : " + query);
    return new Promise(function (resolve, reject) {
        db.transaction(function (transaction) {
            transaction.executeSql(query, [], function (transaction, result) {
                console.log("Test: " + JSON.stringify(result));
                resolve(result); // here the returned Promise is resolved
            }, nullHandler, errorHandler);
        });
    });
}

Upvotes: 2

Related Questions