gpt4 book ai didi

javascript - 在 catch block 中抛出错误 Q Promise 不拒绝 Promise

转载 作者:行者123 更新时间:2023-12-02 13:49:59 25 4
gpt4 key购买 nike

我有以下函数被另一个函数调用:

 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 中拒绝 fetchAllResources catch 处理程序应该被调用,但由于某种奇怪的原因,问题行在 throw 之后不断被调用,并且在被调用十几次(随机)后,它最终拒绝了 processResource< 中的 fetchAllResources 返回的 promise 。我在这里遗漏了一些明显的东西吗?另外请评论我使用 Promise 的风格,可以吗还是我需要更多练习?

最佳答案

您会收到大量日志,因为您使用递归方法,并在每个级别处理和重新抛出错误。同步写的,完全一样

function fetchAll(offset) {
if (offset > 5) throw new Error("message"); // let's say the inner one throws
try {
return fetchAll(offset+1);
} catch(e) {
console.error(e.message);
throw e;
}
}
fetchAll(0);

您也希望在这里收到 5 条消息,对吧?

解决方案是不再处理内部结果中的错误。要通过 promise 实现这一目标,请查看 difference between .then(…).catch(…) and .then(…, …) - 你想要后者:

function fetchAllRecords(client, item, region, offset=1, savedCount=0, 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);
}, function (err) {
// ^^
if (err == 'done')
return 'done';

if (err.message === 'LIMIT_EXCEEDED') {
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
} else {
console.log('Miscellenious error in fetchAllRecords, moving to next resource');
console.log(JSON.stringify(err));
throw new Error('Misc');
}
});
}
function processResource(client, item, debugmode, region) {
if (item.resource === "Property" && region === "ccmls") {
var cities = cities.list;
item.query = item.query + ' ,(City=|' + cities.join() + ')';
}
return fetchAllRecords(client, item, region)
.then(function (result) {
if (debugmode) {
console.log('fetchAllRecords: ' + item.resource + '.' + item.class + ' completed...');
}
return result;
}, function (err) {
console.log(item.resource + '.' + item.class + ' failed with error: ' + JSON.stringify(err));
throw err;
});
}

关于javascript - 在 catch block 中抛出错误 Q Promise 不拒绝 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41087019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com