I wrote a function in Discord.js v13 that expects user input. If the input is invalid, it needs to re-prompt them again, and the original promise should no longer be considered.
If the user takes longer than timeoutSeconds
, then it throws an InputTimeout
error.
What happens in practice, which I don't understand, is this:
- getResponse(...) is executed
- User is prompted for input
- User supplies invalid input
- User is informed of invalid input, and is re-prompted
- If no input is supplied, an
InputTimeout
error is thrown from the first request and the second request
What I wish to happen is:
- getResponse(...) is executed
- User is prompted for input
- User supplies invalid input
- User is informed of invalid input, and is re-prompted
- If no input is supplied, an
InputTimeout
error is thrown for the new request only
Here is what I think is happening... The second prompt is throwing the InputTimeout
error which is causing the catch()
handler from the first Promise to fire off. If that's true, then I am confused, because I thought if then()
runs, then it's because no error was thrown. So why is catch()
catching errors from the then()
function? Should put catch()
first, before then()
? Is this an issue with the order I've chained the functions?
async getResponse(user, timeoutSeconds, channel = null) {
if (channel == null) {
const msg = await user.send(this.promptMessage);
channel = msg.channel;
}
return await channel
.awaitMessages({
max: 1,
time: timeoutSeconds * 1000,
errors: ["time"],
})
.then(async (messages) => {
if (messages.size == 1) {
const message = messages.values().next().value;
if (this.validator(message)) {
await user.send(this.successMessage);
return this.formatter(message);
} else {
await user.send(this.errorMessage);
await this.getResponse(
user,
timeoutSeconds,
channel
);
}
}
})
.catch(async (e) => {
console.error(e);
await channel.send(this.timeoutMessage);
throw new InputTimeout();
});
}