Skip to content Skip to sidebar Skip to footer

Ionic 2 Sqlite Manage Callback With Promise

I want to get the callback of a successful SQLite transaction with Ionic 2. I am still currently learning the HTML5 Promise, and it is still a bit confuse. And since I've heavily u

Solution 1:

I found that it could be added a .bind(this) at the end of a function to carry within that function, the exisiting context (this).

By creating a new Promise() and adding .bind(this) at the end of the function(resolve,reject){} within it, it worked and I can use .then( (...) => {...} on the async result returned.

My function looks like that:

private execSqlCustom = (sqlToExecute:string,bracketValues:Array<any>):Promise<any> => {

    returnnewPromise(function(resolve,reject){
        this.db.transaction(
        function(tx){
            tx.executeSql(sqlToExecute,bracketValues,success,error);
            functionsuccess(tx,rs){
                resolve(rs);                    
            }
            functionerror(tx,error){
                console.log('execSqlCustom error ' + error.message);
                reject(error);
            }
        }
        )}.bind(this)
    );
}

Post a Comment for "Ionic 2 Sqlite Manage Callback With Promise"