React

async, await

hmk run dev 2021. 5. 20. 16:41

async, await 비동기 처리할 때 필요한 친구다...

함수가 특정 함수가 실행된 후에 다른 함수를 실행시키고 싶을 때 아래처럼 써주면 된다!

 

아래 코드는 각각 유저의 마이페이지를 옮겨 다닐떄 (나의 페이지 >> 다른 유저 페이지 or 다른 유저 페이지 >> 다른 유저의 페이지)

 

원래 있던 데이터를 초기화 시켜주고 새로운 데이터를 넣어주기 위해 비동기 처리를 해주었던 코드이다

 

  const initializeApp = async () => {
    await dispatch(profileActions.resetProfile([]));
    await dispatch(storyPostActions.resetStory([]));
    dispatch(profileActions.getUserInfoAPI(userId));
    dispatch(storyPostActions.getUserPostAPI(userId));
    dispatch(storyPostActions.getUserLikeAPI(userId));
  };

현재 나의 마이페이지
버튼을 눌러 다른페이지로 가기
다른 유저의 페이지가 초기화 된후 새로운 데이터를 넣는다