【问题标题】:Is it possible to execute an Anonymous Functions async? [duplicate]是否可以异步执行匿名函数? [复制]
【发布时间】:2022-06-10 17:47:47
【问题描述】:
async function getSomething() {
  const data = await fetch ("https://jsonplaceholder.typicode.com/users")
  const json = await data.json();
  console.log("json",json)
}

getSomething();

问题:是否可以将函数重写为异步匿名函数?

我的尝试:

(async () => {
  const data = await fetch ("https://jsonplaceholder.typicode.com/users")
  const json = await data.json();
  console.log("json",json)
});

【问题讨论】:

  • 你只是在最后一个 ; 之前缺少 () 因为现在你没有执行函数

标签: javascript


【解决方案1】:

你需要在最后使用()调用匿名函数

(async () => {
  const data = await fetch ("https://jsonplaceholder.typicode.com/users")
  const json = await data.json();
  console.log("json",json)
})();

【讨论】:

  • 这肯定属于“错字”近距离投票类别。
猜你喜欢
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 2016-06-22
  • 2019-01-10
  • 1970-01-01
  • 2023-02-07
相关资源
最近更新 更多