使用本机JavaScript Promise进行链异步操作

时间:2014-12-09 20:25:44

标签: javascript promise es6-promise

我已经执行了两次异步操作,然后定义了最终onResultonFault。如何链接两个异步操作getConnection然后select,最后调用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)
}

2 个答案:

答案 0 :(得分: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')
}

答案 1 :(得分: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.
};

另外,我建议您阅读common js promises specification