아핫뉴스실시간 인기검색어
아핫뉴스 화산 이미지
화산 아이콘 11
바빌론 랩스, 애니모카와 비트코인 협력
아하

생활

생활꿀팁

탈퇴한 사용자
탈퇴한 사용자

React javascript Promise 질문 드립니다.

아래처럼 사용하면 가능한데 혹시 then 밖으로 return 할 수 없을까요?? ㅠ.ㅠ 불가능하겠죠...? ㅠ

help(data).then(res=> { console.log(res) });

const help =(data) => { return axios.get( 'URL',{ params: { data, } } ).then(res => res.data) .catch (err => console.error(err)) }

    1개의 답변이 있어요!
    • 꾸준한물범121
      꾸준한물범121

      안녕하세요.

      async await를 사용하시면 리턴문으로 반환하고 받을 수 있습니다.

      function help() { return axios.get( 'URL',{ params: { data, } } ); } async function asyncCall() { try { const res = await help(); console.log(res.data); } catch (err) { console.error(err); } } asyncCall();

      대략 이런 형태로 사용할 수 있습니다.

      https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/async_function