Javascript JQuery延迟解析其他延迟

Javascript JQuery延迟解析其他延迟,javascript,jquery,jquery-deferred,deferred,Javascript,Jquery,Jquery Deferred,Deferred,我有一个函数,它从其他函数推送承诺,这些函数也在解析承诺数组。我只是想知道这个代码是否正确 这里是主要功能 /// <summary>Process the validation results (invalid IDR records will have the iop=4)</summary> this.processValidationResults = function () { var promises = []; var count =

我有一个函数,它从其他函数推送承诺,这些函数也在解析承诺数组。我只是想知道这个代码是否正确

这里是主要功能

    /// <summary>Process the validation results (invalid IDR records will have the iop=4)</summary>
this.processValidationResults = function ()
{
    var promises = [];
    var count = (self.idrValidationList.length - 1);

    $.each(self.idrValidationList, function (index, idrValidationItem)
    {
        _onProgress(self.utils.formatCounterMessage(index, count, 'Processing Validation Items'));

        if (idrValidationItem.is_valid = 0)
        {
            //there is a problem with this IDR record
            //update the idr_insp table
            promises.push(self.updateInvalidEntity(self.configEntities.idr_insp, idrValidationItem.idr_insp_id));
            promises.push(self.updateInvalidChildren(self.configEntities.idr_insp, idrValidationItem.idr_insp_id));
        }
        else
        {
            //push resolved promise
            promise.push($.when());
        }     
    });

    return ($.when.apply($, promises));
}
这链子好吗?我还没有测试过,但我认为还可以。在我继续之前,我想让其他人看看这个…

这应该是真的,不是这样的,但接下来

一些意见:

  • 如前所述,
    .processValidationResults()
    中的进度消息表示发出的请求,而不是收到的响应。因此,它将直接跳到“计数的计数”

  • .processValidationResults()
    中,
    否则{promise.push($.when());}
    是不必要的

  • .updateInvalidChildren()
    中,
    var def=$.Deferred()
    是不必要的

  • jQuery的
    resolve
    reject
    方法是“可分离的”。在
    this.executeSql()
    中,由于您只想传递参数,因此可以简化对
    tx.executeSql(…)
    的调用
    tx.executeSql(sql,params,def.resolve,def.reject)

  • 假设
    self.db.executeSql
    执行AJAX(并且
    tx.executeSql()
    不会以某种方式对请求进行排队),则代码可能会同时请求,严重影响服务器。确保它能够处理此代码将生成的同时请求数。否则,请采取措施使请求按顺序发出


如果你自己都没有测试过的话,仅仅检查某人是否可能发现问题,这不是有点过分了吗?!为什么你不能自己弄清楚这是否有效?@Martin,因为目前正在开发的本地平板电脑数据库中没有加载任何数据。我的任务是编写没有数据的数据同步代码。只是问个问题。我认为这个问题对任何理解尊重的人来说都不难。所以,给自己一个答案,或者把你毫无意义的评论留给自己…相当有攻击性…只是没有时间阅读为什么我不应该发布一个问题。。。很抱歉,谢谢你提供的意见,我现在将在代码回顾中发布。在你的第二颗子弹里,我猜你是说没有必要推空的?因此,如果数组中没有任何内容,$.when.apply将返回已解决的承诺?executeSql是SQLite的aync API的一部分,因此它不会发出服务器请求,只是更新设备上的本地SQLite DB,所以我认为我在这方面还可以。再次感谢您的评论。
$。when()
$。when.apply($,[])
是相同的语句。您的代码已经(正确地)假定
$。when()
返回已解析的承诺。因此,可以完全安全地假设
$.when.apply($,[])
也将返回已解决的承诺。这里的证据:。另一方面,我不确定您是否可以忽略SQL查询过载的可能性。也许SQLite DB可以处理多个rapid fire查询,也许不行-我不知道。确保你的测试制度给你足够的压力,让你满意。再次感谢!单元测试肯定会对SQLite插件造成沉重打击。对于任何给定的父实体,当前DB设计都有少量(最多5个)相关子实体。我们预计在同步的这一特定部分不会有大量的更新,但这也取决于最终用户同步数据的频率,因此您的观点是正确的。
/// <summary>Update the invalid record, sets the IOP field to 4 [Cannot sync due to issue]</summary>
/// <param name="entity" type="Object">GLobal entity definiton </param>
/// <param name="tabletId" type="Int">Primary Key on the tablet to change</param>
this.updateInvalidEnity = function (entity, tabletId)
{
    //update the record with the new ID and IOP status
    var updateSql = 'UPDATE ' + entity.name + ' SET  iop=? WHERE ' + entity.key_field + '=?';

    //update the record
    return (self.db.executeSql(updateSql, [4, tabletId]));
}

/// <summary>Update the invalid child records, sets the IOP field to 4 [Cannot sync due to issue]</summary>
/// <param name="entity" type="Object">GLobal entity definiton </param>
/// <param name="keyId" type="Int">Foreign Key on the tablet to change</param>
this.updateInvalidChildren= function (parentEntity, keyId)
{
    var promises = [];
    $.each(parentEntity.child_entities, function (index, child)
    {
        var def = new $.Deferred();
        var updateSql = 'UPDATE ' + child.child_name + ' SET  iop=? WHERE ' + child.key_field + '=?';

        promises.push(self.db.executeSql(updateSql, [4, keyId]));
    });

    return ($.when.apply($, promises));
}
/* Executes the sql statement with the parameters provided and returns a deffered jquery object */
this.executeSql = function (sql, params)
{
    params = params || [];

    var def = new $.Deferred();

    self.db.transaction(function (tx)
    {
        tx.executeSql(sql, params, function (itx, results)// On Success
        {
            // Resolve with the results and the transaction.
            def.resolve(itx, results);
        },
        function (etx, err)// On Error
        {
            // Reject with the error and the transaction.
            def.reject(etx, err);
        });
    });

    return (def.promise());
}