Javascript 为什么promise.then()和.catch()都被调用?
我开始用discord.js库版本11.5.1编写一个discord机器人。方法返回的承诺无法按预期工作,因为如果成功,then和catch回调都将被调用 我使用的是nodejs版本11.15.0 在本例中,当bot登录时,我在testbot通道中发送一条消息Javascript 为什么promise.then()和.catch()都被调用?,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,我开始用discord.js库版本11.5.1编写一个discord机器人。方法返回的承诺无法按预期工作,因为如果成功,then和catch回调都将被调用 我使用的是nodejs版本11.15.0 在本例中,当bot登录时,我在testbot通道中发送一条消息 const Discord = require('discord.js'); const client = new Discord.Client(); const auth = require('./auth.json'); clien
const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
client.on('ready', () => {
const guild = client.guilds.find(
guild => guild.name === 'test');
const channel = guild.channels.find(
ch => ch.name === 'test-bot');
if (!channel) {
console.error('no channel found');
return ;
}
channel.send('heeeello')
.then(console.log('cool'))
.catch(console.error('grrr'));
});
client.login(auth.token);
消息在discord通道上发送良好,控制台输出为:
cool
grrr
但是我不希望输出中有grrr。then&catch应该接收回调函数,您正在调用console.log
channel.send('heeeello')
.then(() => console.log('cool') )
.catch(() => console.log('grrr') );
这意味着有一个错误…?可能输出错误消息,而不仅仅是“grrr”,您没有将回调函数传递给then和catch。您的console.log表达式将始终执行。我不同意重复标志。我的错误是我给出了一个表达式而不是回调函数。@rems14-我确信您的错误有一个适当的重复项-这是一个非常常见的错误:p这是真的。现在可以了。非常感谢。