Discord.js,异步并等待

时间:2020-08-15 19:05:13

标签: javascript asynchronous async-await discord.js

const getNumberOfQuestions = async () => {
    await this.channel.send('How many questions should I ask? (1-10)')
        .then(async message => {
            await this.channel.awaitMessages(message => message.author.id === this.owner && !isNaN(parseInt(message.content)),  { max: 1, time: 15000 })
                .then(collected => {
                    this.channel.send(`You asked for ${collected.first().content} questions.`);
                    return parseInt(collected.first().content);
                })
                .catch(collected => {
                    this.channel.send('You did not tell me how many questions you wanted. Ending the quiz.');
                });
        });
};

const getDifficulty = async () => {
    await this.channel.send('What difficulty would you like: easy, medium, hard?')
        .then(message => {
            this.channel.awaitMessages(message => message.author.id === this.owner && ['easy', 'medium', 'hard'].includes(message.content.toLocaleLowerCase()),  { max: 1, time: 15000 })
                .then(collected => {
                    this.channel.send(`You asked for ${collected.first().content} difficulty.`);
                    return collected.first().content;
                })
                .catch(collected => {
                    this.channel.send('You did not tell which difficulty you wanted. Ending the quiz.');
                });
        });

};
getNumberOfQuestions();
getDifficulty();

使用上面的代码,我不希望执行在调用此函数之后继续进行。我显然不理解诺言,等待有人可以帮助我吗?

.send.awaitMessages都返回诺言

1 个答案:

答案 0 :(得分:1)

首先,让我重构您的两个同时包含promiseasync / await的过程,以便它们具有等效的async / await

const getNumberOfQuestions = async () => {
    const message = await this.channel.send('How many questions should I ask? (1-10)');
    try {
        const collected = await this.channel.awaitMessages(message => message.author.id === this.owner && !isNaN(parseInt(message.content)),  { max: 1, time: 15000 });
        this.channel.send(`You asked for ${collected.first().content} questions.`);
        return parseInt(collected.first().content);
    } catch(collected) {
        this.channel.send('You did not tell me how many questions you wanted. Ending the quiz.');
    }
};

const getDifficulty = async () => {
    const message = await this.channel.send('What difficulty would you like: easy, medium, hard?');
    try {
        const collected = await this.channel.awaitMessages(message => message.author.id === this.owner && ['easy', 'medium', 'hard'].includes(message.content.toLocaleLowerCase()),  { max: 1, time: 15000 });
        this.channel.send(`You asked for ${collected.first().content} difficulty.`);
        return collected.first().content;
    } catch(collected) {
        this.channel.send('You did not tell which difficulty you wanted. Ending the quiz.');
    }
};

如您所见,await兑现承诺就像将其后的内容视为then内部一样,其中解析的值是await ed表达式返回的值。对承诺的拒绝(.catch()被转换为可以try{...}catch{...}编辑的异常。

知道了这一点,您通过打电话来做什么

getNumberOfQuestions();
getDifficulty();

异步调用这两个函数-无需等待返回的promise解决。相反,您可能想做的是await在第一个结局之前调用第二个结局。像这样:

await getNumberOfQuestions();
await getDifficulty();

但是,await关键字仅在async function中才有意义,因此您可以这样做:

(async()=>{
    await getNumberOfQuestions();
    await getDifficulty();
})();

有关异步功能的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function