我有一个函数,它从其他函数推送承诺,这些函数也在解析承诺数组。我只是想知道这段代码是否可以。
这是主要功能
/// <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));
}
以下是上述函数调用的函数
/// <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());
}
这是连锁声音吗?还没有测试过,但我认为没关系。在我继续之前,只是想让其他人看看这个......
这真的应该在代码审查中,而不是 SO,但这里是......
几点观察:
-
如前所述,
.processValidationResults()
中的进度消息表示已发出的请求,而不是收到的响应。因此,它将直接跳到"计数"。 -
在
.processValidationResults()
,else { promise.push($.when()); }
是不必要的。 -
在
.updateInvalidChildren()
,var def = $.Deferred()
是不必要的。 -
jQuery Deferred的
resolve
和reject
方法是"可分离的"。在this.executeSql()
中,由于您只想传递参数,因此可以简化对tx.executeSql(sql, params, def.resolve, def.reject)
的tx.executeSql(...)
调用。 -
假设
self.db.executeSql
执行 AJAX(并且tx.executeSql()
不会以某种方式对请求进行排队),则代码可能会因同时请求而严重打击服务器。确保它可以处理此代码将生成的并发请求数。否则,请采取措施使请求按顺序发出。