async await try catch

song·2023년 10월 2일
0

js 정보

목록 보기
10/30
const a = async ()=>{
   const b = new Promise((res, rej)=>{
      setTimeout(()=>{
         res(10);
      }, 2000)
   })

   
   try {
      console.log('start!!!');
      const c = await b;	// b함수가 실행될 때까지 기다림. 즉, 동기적으로 변함
      console.log(c);
      console.log('end!!')
   } catch (error) {
      console.log(error);
   }
}

a();
// start!!!
// (2초 후)
// 10
// end!!
profile
인간은 적응의 동물

0개의 댓글

관련 채용 정보