返回课程

Promise 链

编程代码 · JavaScript

💡 Promise 链式调用:fetch 请求 → 解析 → 处理 → 错误捕获
fetch('/api/users')// 发起 HTTP GET 请求到 /api/users 接口
  .then(res => res.json())// .then 将响应体解析为 JSON
  .then(data => console.log(data))// .then 打印解析后的数据
  .catch(err => console.error(err))// .catch 捕获请求过程中的任何错误
  .finally(() => setLoading(false));// .finally 无论成功失败都执行(关闭加载状态)
0 字/分0% 准确
🐵
f左手食指
ESC 暂停 退格