检测异步任务何时完成

时间:2014-02-25 08:26:38

标签: javascript sqlite asynchronous

我想检测这个异步操作列表何时完成,以及在其他操作中使用插入的条目。

    var peers = this.response["peers"];
    $.each(peers, function(key, value) {
        that.database.transaction(function(tx) {
            var user_id = parseInt(value["user_id"]);
            var username = value["username"];
            var picture = value["picture"];
            var date_of_birth = value["date_of_birth"];
            var score = parseInt(value["score"]);
            var country = value["country"];
            var last_seen = value["last_seen"];
            var is_favorite = value["is_favorite"];
            var is_match = value["is_match"];
            var is_blocked = value["is_blocked"];
            var is_auto_match = value["is_auto_match"];
            var is_profile_viewer = value["is_profile_viewer"];
            var sql = "insert into users(user_id,username,picture,";
            sql += "date_of_birth,score,country,last_seen,is_blocked,";
            sql += "is_favorite,is_match,is_auto_match,is_profile_viewer)";
            sql += "values (?,?,?,?,?,?,?,?,?,?,?,?)";
            tx.executeSql(sql, [user_id, username, picture, date_of_birth, score, country, last_seen, is_blocked, is_favorite, is_match, is_auto_match, is_profile_viewer]);
        });
    });

1 个答案:

答案 0 :(得分:2)

如果你不能使用任何异步技术,那么你可以这样做

var peers = this.response["peers"];
var total = peers.length;
$.each(peers, function(key, value) {
    that.database.transaction(function(tx) {
        ...
        tx.executeSql(sql, [...], function() {
            total -= 1;
            if (!total) {
                otherOperation(...); // Invoke the operation after all data done
            }
        });
    });
});