개발하는 루루언니

javascript : 콜백함수 연습하기 : 콜백함수안에 콜백함수 본문

컴퓨터 정보/javascript

javascript : 콜백함수 연습하기 : 콜백함수안에 콜백함수

혜닝혜루 2022. 11. 26. 21:37
728x90
반응형

 

https://hyeru.tistory.com/123

 

javascript 콜백함수 연습하기

const askForLocation = function (){ navigator.geolocation.getCurrentPosition((position)=>{ console.log(position) } ) } askForLocation() 가장 나에게 헷갈림을 주었던 콜백함수,,연습이 살길인가? 나는 위치 api를 받아오고 싶었

hyeru.tistory.com

https://hyeru.tistory.com/124

 

javascript : 콜백함수 연습하기 (심화)

내가 콜백함수 연습하기 블로그에서 쓴 코드이다. const askForLocation = function (){ navigator.geolocation.getCurrentPosition((position)=>{ console.log(position) } ) } askForLocation() 콜백함수 지옥이 다시 왔다...이해했드

hyeru.tistory.com

 

지난 글에 이어 이번엔 콜백함수 안에 콜백함수 넣기이다.

 

이건 생각보다 어렵진 않았다.

 

const accessToGeo = function (position){
 console.log(position)
}


// 현재 위치 가져오기 api = 
const askForLocation = function (){
  navigator.geolocation.getCurrentPosition(accessToGeo, (err)=>{
    console.log(err)
  })
}
askForLocation()

 

accessToGeo 함수 옆에 컴마 (,)  하고 (err) => { } 똑같이 콜백함수를 넣어주면 된다.

저 err 는 콘솔로 찍어서 확인해보면 

이렇게 위치 엑세스를 차단을 했을때 나오는걸 err로 콜백함수를 주게되면 어케 되는지 보기위함이다.

 

콘솔로 찍어보면 이렇게 에러 문구가 나오는걸 알 수 있다.

그럼 우리의 위도 경도 등..뭐내용들을 쓰지 못하는것이다.

728x90