为什么捕获此错误会破坏映射函数和承诺链?
Why does catching this error break the mapping function & promise chain?
我正在尝试从一个房间中提取多条消息。某些消息可能已被删除,因此尝试获取它们会导致错误...
const keyPromises = data.map(async(pack) => {
return new Promise((resolve, reject) => {
console.log("Checking " + pack.msgID);
client.guilds.cache.find(g => g.name === mainGuild).channels.cache.find(c => c.name === 'room').messages.fetch(pack.msgID)
.then(fetched => {
// Nothing is printed after catching the first error... Why?
console.log(fetched.attachments.first().url);
resolve(fetched.attachments.first().url + '\n');
})
.catch(() => {
console.log("> " + pack.msgID + " was not found!");
resolve("");
// it doesn't continue checking the other ids after catching the first error. Why?!
});
});
});
问题是映射函数在捕捉到第一个错误后挂起,即使它应该继续执行...
输出:
Checking 707897580784320605
Checking 707899109477974037
Checking 710851524217143336
Checking 716151973291884575
> 707897580784320605 was not found!
提前致谢
我的速率因不和谐而受到限制。我通过在承诺中添加超时来解决问题。谢谢大家。
我正在尝试从一个房间中提取多条消息。某些消息可能已被删除,因此尝试获取它们会导致错误...
const keyPromises = data.map(async(pack) => {
return new Promise((resolve, reject) => {
console.log("Checking " + pack.msgID);
client.guilds.cache.find(g => g.name === mainGuild).channels.cache.find(c => c.name === 'room').messages.fetch(pack.msgID)
.then(fetched => {
// Nothing is printed after catching the first error... Why?
console.log(fetched.attachments.first().url);
resolve(fetched.attachments.first().url + '\n');
})
.catch(() => {
console.log("> " + pack.msgID + " was not found!");
resolve("");
// it doesn't continue checking the other ids after catching the first error. Why?!
});
});
});
问题是映射函数在捕捉到第一个错误后挂起,即使它应该继续执行...
输出:
Checking 707897580784320605
Checking 707899109477974037
Checking 710851524217143336
Checking 716151973291884575
> 707897580784320605 was not found!
提前致谢
我的速率因不和谐而受到限制。我通过在承诺中添加超时来解决问题。谢谢大家。