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