为什么 promise .then() 和 .catch() 都被调用?

-3 javascript node.js discord.js

我开始用 Discord.js 库(版本 11.5.1)编写一个不和谐的机器人。方法返回的 Promise 没有按预期工作,因为 then() 和 catch() 回调在成功时都会被调用。

我正在使用 nodejs 版本 11.15.0。

在这个例子中,当机器人登录时,我在 test-bot 频道中发送一条消息。

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);
Run Code Online (Sandbox Code Playgroud)

消息在不和谐频道上很好地发送,控制台输出是:

cool
grrr
Run Code Online (Sandbox Code Playgroud)

但我不期望grrr在输出中。

Tar*_*sam 5

then & catch 应该接收一个回调函数,你正在调用console.log。

channel.send('heeeello')
.then(() => console.log('cool') )
.catch(() => console.log('grrr') );
Run Code Online (Sandbox Code Playgroud)