【发布时间】:2016-12-12 01:31:48
【问题描述】:
我有以下函数被另一个函数调用:
function fetchAllRecords(client, item, region, offset, savedCount, totalCount) {
offset = offset || 1;
savedCount = savedCount || 0;
totalCount = totalCount || 0;
return processQueue(client, item, offset, region).then(function (result) {
return associate(result, item, region)
}).then(function (success) {
return saveBatch(item, success.allResources, region);
}).then(function (result) {
savedCount += result.savedCount;
totalCount += result.totalCount;
if (debugmode) {
console.log(savedCount + '/' + totalCount);
}
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}).catch(function (err) {
if (err == 'done')
return 'done';
// All of the above steps have built-in retry so we assume there's a non-recoverable error in this batch and move to next
if (err.message === 'LIMIT_EXCEEDED') {
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}
else {
******************* Problem Line ********************
console.log('Miscellenious error in fetachAllRecords, moving to next resource');
console.log(JSON.stringify(err));
throw new Error('Misc');
}
});
}
它在另一个函数中被这样调用
function processResource(client, item, debugmode, region) {
var deferred = q.defer();
if (item.resource === "Property" && region === "ccmls") {
var cities = cities.list;
item.query = item.query + ' ,(City=|' + cities.join() + ')';
}
fetchAllRecords(client, item, region)
.then(function (result) {
if (debugmode) {
console.log('fetchAllRecords: ' + item.resource + '.' + item.class + ' completed...');
}
deferred.resolve(result);
})
.catch(function (err) {
console.log(item.resource + '.' + item.class + ' failed with error: ' + JSON.stringify(err));
deferred.reject(err);
});
return deferred.promise;
}
在上面的问题行中,它应该拒绝fetchAllRecords,而在processResource 中,应该调用fetachAllResources catch 处理程序,但是由于某种奇怪的原因,问题行在 throw 之后和之后一直被调用被调用了十几次(随机),它最终拒绝了fetchAllResources 在processResource 中返回的承诺。
我在这里遗漏了一些明显的东西吗?另外请评论一下我使用 Promise 的风格,可以吗?还是我需要更多练习?
【问题讨论】:
标签: javascript promise q