Search code examples
node.jsdiscord.jses6-promise

Why does catching this error break the mapping function & promise chain?


I'm trying to fetch multiple messages from a room. Some messages may have been deleted so attempting to fetch them would result in an error...

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?!
        });
    });
});

The problem is that the mapping function hangs after catching the first error even though it should continue executing...

The output:

Checking 707897580784320605
Checking 707899109477974037
Checking 710851524217143336
Checking 716151973291884575
> 707897580784320605 was not found!

Thanks in advance


Solution

  • I was getting rate limited by discord. I solved the problem by adding timeouts into the promises. Thanks everyone.