0

我已经进行了两个异步操作,然后进行了最终onResultonFault定义。如何链接两个异步操作getConnectionselect然后最后调用onResultonFault

编辑需要帮助来承诺以下序列。

    new Promise(this.getConnection)
        .then(this.select)
        .then(this.onResult)

getConnection: function(resolve, reject) {
    console.log('get connection')
    if(database = common.model.connections.Sync.getConnection()) {
        database.transaction(function(transaction){
            resolve(transaction);
        });
    } else {
        reject("Connection Error");
    }
},

select: function(transaction) {
    console.log('select', transaction)
    var self = this;

    var query = "SELECT * FROM configuration WHERE key = 'schedule'";
    self.transaction.executeSql(query, [], function(transaction, resultSet){self.selectTransactionComplete(transaction, resultSet)}, function(){self.selectTransactionError()});
},

selectTransactionComplete: function(transaction, resultSet) {
    console.log('select transaction complete')
    if(resultSet.rows.length == 0) {
        this.onResult(false);
    } else if(new Date().getTime() - new Date(common.Config.getLastSync()).getTime() > resultSet.rows.item(0).value) {
        this.onResult(true);
    }
},

selectTransactionError: function(error) {console.log(this.constructor.NAME, this.selectSQL); console.log(error);},

onResult: function(data) {
    console.log(data);
},

onFault: function(info) {
    console.log(info)
}
4

2 回答 2

0

在尝试了几件事之后,这是应该怎么做的?请查阅

    this.getConnection()
        .then(this.select, this.getConnectionError)
        .then(this.selectTransactionComplete, this.selectTransactionError)

getConnection: function() {
    console.log('get connection')

    var promise = new Promise(function(resolve, reject){
        try {
            database = common.model.connections.Sync.getConnection();
            database.transaction(function(transaction){
                resolve(transaction);
            });
        } catch(error) {
            reject("Connection Error");
        }
    });

    return promise;
},

getConnectionError: function(info) {
    console.log("connectionError");
    this.onFault();
},

select: function(transaction) {
    console.log('select')
    var self = this;

    var promise = new Promise(function(resolve, reject){
        var query = "SELECT * FROM configuration WHERE key = 'schedule'";
        transaction.executeSql(query, [], function(transaction, resultSet){resolve(resultSet)}, function(error){reject(error)});
    });

    return promise;
},

selectTransactionComplete: function(resultSet) {
    console.log('selectTransactionComplete')

    /*if(resultSet.rows.length == 0) {
        this.onResult(false);
    } else if(new Date().getTime() - new Date(common.Config.getLastSync()).getTime() > resultSet.rows.item(0).value) {
        this.onResult(true);
    }*/
},

selectTransactionError: function(error) {
    console.log('selectTransactionError')
    //console.log(this.constructor.NAME, this.selectSQL);
    //console.log(error);
},

onResult: function(data) {
    console.log('onResult')
},

onFault: function(info) {
    console.log('onFault')
}
于 2014-12-09T21:24:49.267 回答
0

如果您使用像q这样的承诺库,则链接承诺的方式如下;

getConnection: function() {
    var deferred = Q.Defer();
    console.log('get connection')

        try {
            database = common.model.connections.Sync.getConnection();
            database.transaction(function(transaction){
                deferred.resolve(transaction);
            });
        } catch(error) {
            deferred.reject("Connection Error");
        }

    return deferred.promise;
}

当您打电话时,您会执行以下操作;

Q.when(getConnection)
.then(function(result){
 // handle success or resolve
}, function(error){
 // handle rejection.
};

另外我建议阅读常见的 js 承诺规范

于 2014-12-09T22:06:15.557 回答