[TIL] 220907 167일차

youngseo·2022년 9월 7일
0

TIL

목록 보기
121/121

167일차

  • 파이널 프로젝트 이슈 정리하기

새로배운 내용

  • api호출 함수 작성시 유의하면 좋을 부분
    • 에러핸들링
    • 비동기 호출을 일반화하기
/*기존 함수
const getProductData = async () => {
  const response = await fetch('./api/productData.json');
  const data = await response.json();
  return data;
};
**/

//함수 일반화 + 에러핸들링
  const request = async (url) => {
  try {
    const response = await fetch(url);
    if (response.ok) {
      const data = await response.json();
      return data;
    }
    const errData = await response.json();
    throw errData;
  } catch (e) {
    console.log(e);
  }
};

//일반화된 함수 사용
//일반화된 함수를 사용하는 경우 url의 엔드포인트를 변경할 수도 있습니다.
const getProductData = async () => {
  const result = await request(BASIC_URL);
  return result;
};

const getProductData = async () => {
  const result = await request(CART_URL);
  return result;
};

0개의 댓글