【问题标题】:Async message or (message)? - discord.js [duplicate]异步消息还是(消息)? - discord.js [重复]
【发布时间】:2020-08-20 16:43:49
【问题描述】:

我有一个问题。 async message 和只有 (message) 有什么不同。 这是异步的示例 client.on('message', async message => {CODE}

这是一个没有异步 client.on('message', (message) => {CODE}) 的示例

希望你能理解我的问题;)

【问题讨论】:

  • 如果只有一个参数,箭头函数参数的括号可以省略。 (message) => === message =>.

标签: javascript discord discord.js


【解决方案1】:

(message) => {}arrow functionasync message => {}async function

本质区别在于,在async 函数中,您可以await 进行异步代码完成。

正如您在mdn page for arrow function expressions 中看到的那样:

// Parentheses are optional when there's only one parameter name:
(singleParam) => { statements }
singleParam => { statements }

所以

async message => {CODE}
// is equal to
async (message) => {CODE}

(message) => {}
// is equal to
message => {}

重要的是在使用括号的方式上保持一致,而在代码中实现 100% 一致性的最佳方法是使用像 prettier 这样的工具。

【讨论】:

  • 什么对不和谐机器人更好?
  • @verleuchtet 两者都很有用。这取决于你想做什么。
猜你喜欢
  • 2021-07-30
  • 2021-08-16
  • 2015-04-12
  • 2014-11-01
  • 2015-11-25
  • 2022-06-10
  • 2019-11-17
  • 1970-01-01
  • 2021-01-13
相关资源
最近更新 更多