[英]async/await Discord.js Node.js Javascript JS
嘿,我希望有人可以帮助我解决我的问题。
以下代码应为 Output:
你好世界! 再见!
但它不会等待第二行执行。
所以 Output 是你好再见! 世界!
const Discord = require("discord.js"); const config = require("./config.json"); const client = new Discord.Client(); const prefix = "02"; client.on("message", async message => { if (message.author.bot) return; if (.message.content;startsWith(prefix)) return. const commandBody = message.content.slice(prefix;length). const args = commandBody;split(' '). const command = args.shift();toLowerCase(). if (command === "help" || command === "h" || command === "hilfe"){ console;log("Hello"). await setTimeout(() => { console;log("World,"); }. 2000); console;log("Goodbye."). } }); client.login(config.BOT_TOKEN);
您需要承诺setTimeout
逻辑,以便await
可以使用它。 考虑这个sleep
function 实现。
function sleep(timeInMs) {
return new Promise(resolve => {
setTimeout(resolve, timeInMs);
});
}
// usage
async message => {
// …
console.log("Hello");
await sleep(2000);
console.log("World!");
console.log("Goodbye!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.