const request = async (url, params = {}, method = 'GET', headers = { 'Content-Type': 'application/json' }) => {
  // 1. 创建控制器
  const controller = new AbortController();
  const { signal } = controller;
  
  // 2. 设置定时器，超时后调用 abort()
  const timeoutId = setTimeout(() => {
    controller.abort(); // 触发中止信号
  }, 5000)
  
  const res = await fetch(url, {
      method: method,
      headers: headers,
      body: JSON.stringify(params),
      signal
  }).finally(() => {
      // 4. 无论请求成功还是失败，都要清除定时器，防止内存泄漏
      clearTimeout(timeoutId);
    });

  const data = await res.json()
  return data
}

export default request