高级前端
react
【Q624】同一页面三个组件请求同一个 API 发送了三次请求,如何优化

同一页面三个组件请求同一个 API 发送了三次请求,如何优化

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 642 (opens in a new tab)

Author 回答者: shfshanyue (opens in a new tab)

const fetchUser = (id) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("Fetch: ", id);
      resolve(id);
    }, 5000);
  });
};
 
const cache = {};
const cacheFetchUser = (id) => {
  if (cache[id]) {
    return cache[id];
  }
  cache[id] = fetchUser(id);
  return cache[id];
};
cacheFetchUser(3).then((id) => console.log(id))
cacheFetchUser(3).then((id) => console.log(id))
cacheFetchUser(3).then((id) => console.log(id))
 
// Fetch:  3
// 3
// 3
// 3