小编Lea*_*dro的帖子

我应该将 fetch() 与 .then 一起使用还是 async/await

以下代码片段给了我相同的结果。使用.thenasync/await来获取数据有什么区别?

// Code 1

function fetchData() {
  fetch(url)
      .then(response => response.json())
      .then(json => console.log(json))
}



// Code 2

async function fetchData() {
  const response = await fetch(url);
  const json = await response.json();
  console.log(json);
}
Run Code Online (Sandbox Code Playgroud)

javascript fetch promise async-await

7
推荐指数
1
解决办法
5524
查看次数

标签 统计

async-await ×1

fetch ×1

javascript ×1

promise ×1