- 비동기 처리 함수
- Promise 와 동일한 기능에 간결
- Promise를 포함하고 있음
- async 함수의 반환값은 Promise 

 

//=====================

fetch 비동기 실행 예제
https://dev.to/shoupn/javascript-fetch-api-and-using-asyncawait-47mp

 

async function getUserAsync(name) 
{
  let response = await fetch(`https://api.github.com/users/${name}`);
  let data = await response.json()
  return data;
}

getUserAsync('yourUsernameHere')
  .then(data => console.log(data)); 

 

 

//=====================
예제) 
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/async_function
//=======================================
function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  var result = await resolveAfter2Seconds();
  console.log(result);
  // expected output: 'resolved'
}

asyncCall();
//=======================================

async function name([param[, param[, ... param]]]) { 
    statements
}





//====================================
// 참고

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

https://blueshw.github.io/2018/02/27/async-await/

https://medium.com/@constell99/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%9D%98-async-await-%EA%B0%80-promises%EB%A5%BC-%EC%82%AC%EB%9D%BC%EC%A7%80%EA%B2%8C-%EB%A7%8C%EB%93%A4-%EC%88%98-%EC%9E%88%EB%8A%94-6%EA%B0%80%EC%A7%80-%EC%9D%B4%EC%9C%A0-c5fe0add656c


반응형
Posted by codens