ionic 2 SQLite使用Promise管理回调 [英] ionic 2 SQLite manage callback with Promise

查看:134
本文介绍了ionic 2 SQLite使用Promise管理回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Ionic 2获得成功的SQLite事务的回调.我目前仍在学习HTML5 Promise,但仍然有些困惑.而且由于我以前已经大量使用了jQuery Deferred,所以我尝试适应我以前使用jQuery所做的事情.

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 used jQuery Deferred before, I try to adapt what I used to do with jQuery.

在Ionic 2之前,我使用以下jQuery模式执行异步SQL:

Before Ionic 2, I used the following jQuery pattern to execute my async SQL:

var SqlResult = function(sqlToExecute,bracketValues){
    this.sqlToExecute = sqlToExecute;
    this.bracketValues =bracketValues;
};

SqlResult.prototype.execSqlCustomDeferred = function(){
        var execSqlCustomDeferredRes = $.Deferred();
        var execSqlCustomDeferredResProm = execSqlCustomDeferredRes.promise();  

        var sqlToExecuteForTx = this.sqlToExecute;
        var bracketValuesForTx = this.bracketValues;

        DbManagement.db.transaction(function(tx){
            tx.executeSql(sqlToExecuteForTx,bracketValuesForTx,success,error);
            function success(tx,rs){
                    execSqlCustomDeferredRes.resolve(rs);
            }
            function error(tx,error){
                execSqlCustomDeferredRes.reject(error);
            }
        });
        return execSqlCustomDeferredResProm;
};

使用这种模式,我得到的是异步结果,使用SqlResult.prototype.execSqlCustomDeferred.done(function(res){...}).

With that pattern I was getting the async result, with SqlResult.prototype.execSqlCustomDeferred.done(function(res){...}).

现在,我很难弄清楚应该如何使用Promise模式订购事物,我想到了以下内容,但与之不符:

Now I struggle to figure out how things should be ordered to use the Promise pattern, I thought of the following but it does not match:

这种情况将不起作用,因为resolve()reject()不存在(它不会进行转译).

This case would not work because resolve() or reject() don't exist (it doesn't transpile).

private execSqlCustom = (sqlToExecute:string,bracketValues:Array<any>) => {
    let sqlToExecuteForTx:string = sqlToExecute;
    let bracketValueForTx:Array<any> = bracketValues;

    return this.db.transaction(
        function(tx){
            tx.executeSql(sqlToExecuteForTx,bracketValueForTx,success,error);
            function success(tx,rs){
                resolve(rs);                    
            }
            function error(tx,error){
                console.log('execSqlCustom error ' + error.message);
                reject(error);
            }
        }
    );

这也不起作用,因为我认为我会丢失上下文(this)(它会转换,但在执行时会告诉我:

This would not work neither because I think I'd lose the context (this) (it transpiles but at execution it tells me that:

它无法读取未定义的数据库

it cannot read db of undefined

):

private execSqlCustom = (sqlToExecute:string,bracketValues:Array<any>) => {
    let sqlToExecuteForTx:string = sqlToExecute;
    let bracketValueForTx:Array<any> = bracketValues;
    return new Promise(function(resolve,reject){
       this.db.transaction(
        function(tx){
            tx.executeSql(sqlToExecuteForTx,bracketValueForTx,resolve,reject);
        }
    );

有人发现我在做什么错吗?

Does anyone spot what I've been doing wrong?

推荐答案

我发现可以在函数的末尾添加.bind(this)以便在该函数(现有上下文)中进行携带.

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).

通过创建new Promise()并在其中的function(resolve,reject){}末尾添加.bind(this),它可以正常工作,我可以在返回的异步结果上使用.then( (...) => {...}.

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.

我的功能如下:

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

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

这篇关于ionic 2 SQLite使用Promise管理回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆